knowbody
knowbody

Reputation: 29

How to remotely change a service account password on multiple servers

I've been searching through the archives here but haven't quite found a simple (ie: something I can understand) solution to my problem. I am changing passwords on service accounts using powershell and wmi. I can change the services one at a time across all servers like so:

$Service = Get-WmiObject -Class Win32_Service -computer REMOTESERVER -filter "name='SERVICENAME'"
$service.change($null,$null,$null,$null,$null,$null,$null,"newpasswordhere")

As you can see, I can refer to whatever server I want and whatever service on that server. What I'd like to do is the following

  1. Provide a list of servers (a text file with "REMOTESERVER1,REMOTESERVER2" or something similar
  2. Change the password for multiple services on the same machine that are running under the same credentials. I was able to get a list of mutliple services using -filter "StartName LIKE '%\MYSERVICEACCOUNT'", but when I then try to run the $service.change to update the password, I get an error

Method invocation failed because [System.Object[]] doesn't contain a method named 'change'. At line:1 char:16 + $service.change <<<< ($null,$null,$null,$null,$null,$null,$null,"newpasswordhere") + CategoryInfo : InvalidOperation: (change:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

It works fine with only one service though

  1. Restart services only if they are already running

How can I enhance this script to handle these 3 additional items?

Thank you

Upvotes: 2

Views: 3138

Answers (1)

Luke
Luke

Reputation: 667

You would want to add a loop to your above script, and you would want

    param
    (
    [string]$File
)
$Computer = Get-Content $file

foreach ($i in $Computer){

$Service = Get-WmiObject -Class Win32_Service -computer $i -filter "name='SERVICENAME'"
$service.change($null,$null,$null,$null,$null,$null,$null,"newpasswordhere")
}

You add the Param so you can utilize this many different times, then the foreach loop will run through your code for each computer in your .txt file.

Now this only answers the first part of your question but should give you a good starting point.

Upvotes: 1

Related Questions