如何使用远程Powershell重置域用户密码

时间:2015-12-03 08:35:25

标签: powershell active-directory remoting

我想在远程计算机(不同的域)上重置我自己的Active Directory密码。

如果我在本地使用以下代码段,则效果非常好:

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)

在远程计算机上运行以下代码段失败:

$ADSystemInfo = New-Object -ComObject ADSystemInfo
$type = $ADSystemInfo.GetType()
Invoke-Command -Session $Session -ScriptBlock {
    param($rtype, $RemoteADSystemInfo, $OldPassword)
    $user = [ADSI] "LDAP://$($rtype.InvokeMember('UserName', 'GetProperty', $null, $RemoteADSystemInfo, $null))"
    $user.ChangePassword($OldPassword , "TestPa$$w0rd"")
} -ArgumentList $type,$ADSystemInfo,$Password
Error Message: Method invocation failed because
[Deserialized.System.RuntimeType] does not contain a method named
'InvokeMember'.
    + CategoryInfo          : InvalidOperation: (InvokeMember:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
    + PSComputerName        : test.test.domain   The following exception occurred while retrieving member "ChangePassword": "Unknown error (0x80005000)"
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember
    + PSComputerName        : test.test.domain

0 个答案:

没有答案