I used this method to change the service login to local system for about 40 machines. Your server list file could be .txt, .csv or some other powershell get function.
$svcobj.change relies on these these values in this sequence below. Each value here corresponds to a specific portion of the service configuration. There are 11 values and $svcobj.change needs to have all of them configured in order to work properly. Ensure you put them in the proper order, otherwise you could corrupt the service configuration. To change to LocalSystem, StartName should be changed to "LocalSystem" and all other values remain as $null.
<# WMI Win32_Service values
uint32 Change(
[in] string DisplayName,
[in] string PathName,
[in] uint32 ServiceType,
[in] uint32 ErrorControl,
[in] string StartMode,
[in] boolean DesktopInteract,
[in] string StartName,
[in] string StartPassword,
[in] string LoadOrderGroup,
[in] string LoadOrderGroupDependencies[],
[in] string ServiceDependencies[]
);
#>
#Configure Runas LocalSystem
$svcname = "WindowsService"
foreach ($source in get-content "Some_Path_to_Server_List") {
$source
$svcobj = Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
$svcobj.StopService | out-null
$svcobj.Change($null,$null,$null,$null,$null,$null,"LocalSystem",$null,$null,$null,$null) | out-null
$svcobj.StartService | out-null
Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
}
Or you can use this code below to run as a specific user with username and password. $svccred prompts for username and password and stores it encrypted in memory.
#Configure Runas AD User
$svccred = Get-Credential -Message "Enter Service Account Credentials"
$svcname = "WindowsService"
foreach ($source in get-content "Some_Path_to_Server_List") {
$source
$svcobj = Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
$svcobj.StopService | out-null
$svcobj.Change($null,$null,$null,$null,$null,$null,$svccred.Username,$svccred.Password,$null,$null,$null) | out-null
$svcobj.StartService | out-null
Get-WmiObject -ComputerName $source -Class Win32_Service -Filter "Name='$svcname'"
}