0

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
0

1 Answer 1

1

passing variables into scriptblock should be like this :

$new_password = "mypassword"
Invoke-Command -ComputerName $computer  -Credential $mycred -ScriptBlock {param($new_password) (([ADSI]“WinNT://localhost/Administrator,user”).SetPassword($new_password)).SetInfo } -argumentlist $new_password

and the adsi should be ([ADSI]“WinNT://localhost/Administrator,user”)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.