以下是一些PowerShell代码,可以使用域帐户执行您正在寻找的内容:
param (
[string]$oldPassword = $( Read-Host "Old password"),
[string]$newPassword = $( Read-Host "New password")
)
$ADSystemInfo = New-Object -ComObject ADSystemInfo
$type = $ADSystemInfo.GetType()
$user = [ADSI] "LDAP://$($type.InvokeMember('UserName', 'GetProperty', $null, $ADSystemInfo, $null))"
$user.ChangePassword( $oldPassword, $newPassword)
ASDI提供程序还支持ChangePassword()方法的语法WinNT:// computername / username.但是,ADSystemInfo对象不适用于机器本地帐户,因此只使用WinNT:// …语法改进上述代码是不可行的.
(任何人都想建议编辑w /代码来区分本地和域帐户?)
在完全不同的方面,旧的NetUserChangePassword API也适用于本地(和域,如果您在NetBIOS语法中指定域名)帐户:
param (
[string]$oldPassword = $( Read-Host "Old password"),
[string]$newPassword = $( Read-Host "New password")
)
$MethodDefinition = @'
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
public static extern bool NetUserChangePassword(string domainname, string username, string oldPassword, string newPassword);
'@
$NetAPI32 = Add-Type -MemberDefinition $MethodDefinition -Name 'NetAPI32' -Namespace 'Win32' -PassThru
$NetAPI32::NetUserChangePassword('.', $env:username, $oldPassword, $newPassword)
此代码假定您正在更改本地计算机上的密码(“.”).