0
votes

Powershell question.

I am trying to change local user password on multiple remote servers that are on different domains. The csv file contains 4 columns: servername, domain, password, localuser

Example:

  • Server1, mydomain.dev, test123, localadmin
  • Server2, mydomain.uat, test123, localadmin
  • Server3, mydomain.prod test123, localadmin

Here is what i have so far but sadly with no success:

Import-CSV servers.csv | ForEach-Object {Invoke-Command -ComputerName "$($_.servername).$($_.domain)" `
-Credential "$($_.domain)\$($env:username)" {([adsi]"WinNT://./$_.localuser").SetPassword($_.password)}
    }

Being fairly new to Powershell I am at a loss how to pass locauser and password properties inside {} properly.

Any help is greatly appreciated!

1
Thanks @GuentherSchmitz. I tried adding: {param(l_user,$l_pass)([adsi]"WinNT://./$l_user").SetPassword($l_pass)} -ArgumentList $_.localuser,$_.password. It does not seem to work but am I on the right path? - Norskyi
Please disregard my previous comment. I was breaking line incorrectly and that was the cuplit - Norskyi

1 Answers

0
votes

Thanks to @GuentherSchmitz direction, the following worked for me.

Import-CSV servers.csv |
   ForEach-Object {Invoke-Command -ComputerName "$($_.servername).$($_.domain)"-Credential "$($_.domain)\$($env:username)" {
   param($l_user,$l_pass) ([adsi]"WinNT://./$l_user").SetPassword("$l_pass")} -ArgumentList $_.localuser,$_.password
   }