0

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!

Norskyi
  • 155
  • 13
  • 1
    this should clearify things https://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command#4226027 – Guenther Schmitz Nov 04 '19 at 06:29
  • 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 Nov 04 '19 at 07:38
  • Please disregard my previous comment. I was breaking line incorrectly and that was the cuplit – Norskyi Nov 04 '19 at 08:26

1 Answers1

0

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
   }
Norskyi
  • 155
  • 13