The following function will:
Connect to the service that it’s trying to change credentials on through WMI
Change the credentials to those provided when calling the function
Stop the service, wait until the service is stopped (while re-enumerating the service to get up to date WMI information)
… and then start the service again.
If you have any issues with the script, feel free to comment below and I will work to add some error handling or additional functionality. Also, WMI can sometimes hang when a server is in a bad state. If this happens please look for a custom WMI function with connection options specified. If someone needs that, again – comment and I will post some sample code.
As always, enjoy!
$filter = 'Name=' + "'" + $strServiceName + "'" + ''
$service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
$service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter}
Usage: Set-ServiceAcctCreds -strCompName “Computer1″ -strServiceName “Service” -newAcct “DOM\ServiceUser” -newPass ‘newSecureWord’