Thursday, September 20, 2018

PowerShell script to reset service account password and restart services on multiple servers.

Need: You need to reset the password for a service account on one or more services on multiple servers at the same time, and restart the impacted services.

Prerequisite: Create a txt file with all the servers that are using a specific service account for one or more services.  Create a column header of "servers" and just paste all the servers names below.You can compile this file from my earlier blogpost here.

Script:

$newpass = "ReallyLongPasswordYouWouldHateTyping"
Import-csv C:\data\computers.txt|foreach-object {
$server = $_.servers
$services = get-wmiobject win32_service -ComputerName $server |?{$_.Startname -like "*svcaccountname*"}
foreach($service in $services)
{$service.change($null,$null,$null,$null,$null,$null,$null,$newpass)
get-service $service.name -ComputerName $server|stop-service
start-sleep -s 30
get-service $service.name -ComputerName $server|start-service
}}