I'm automating changing passwords on remote PowerShell targets using an ADSI command I send through Invoke-Command. I'm using the following PowerShell command to send the ADSI command to the remote machine:
Invoke-Command -ComputerName $computer -ScriptBlock {
(([ADSI]“WinNT://localhost/Administrator”).SetPassword($new_password)).SetInfo
} -Credential $mycred
Everything works except for the $new_password variable. If I enter a string directly in the ADSI command (in the SetPassword() method), the password is changed successfully on the remote client. However if I use the $new_password variable instead, then the administrator password on the remote client gets set as blank. All other variables, i.e $computer and $mycred seem to work properly in the entire Invoke-Command command, however it's only that one $new_password that isn't working. Even if I do the following:
$new_password = "mypassword"
Invoke-Command -ComputerName $computer -ScriptBlock {
(([ADSI]“WinNT://localhost/Administrator”).SetPassword($new_password)).SetInfo
} -Credential $mycred