Advertisement
mikedopp

ChangeServiceAccount.ps1

Sep 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clear
  2. # New Service Account
  3. $ServiceAccount = "<Username>"  
  4. $ServicePassword = "<Password>"
  5.  
  6. # Array of Services which should be changed, Use the real "Display Name" of each Service
  7. $Srvs = ('Sophos Agent', 'Adobe Acrobat Update Service');
  8.  
  9. #Prompt for an administartiv user name and password
  10. $Cred = Get-Credential
  11.  
  12. #Read the servers listed in the server.txt file located in the script folder
  13. $servers = Get-Content "C:\Powershell\server.txt"
  14. foreach ($server in $servers)
  15.     {
  16.     # Stop, Configure and Restart of the listed Services above
  17.     foreach ($Srv in $Srvs)
  18.         {
  19.         $gsrv = Get-Service -DisplayName $Srv
  20.         Write-Host "Service $Srv on $server" -foreground "Green";
  21.         Stop-Service -DisplayName $gsrv -Force
  22.         Set-Service -InputObject $gsrv -StartupType Automatic
  23.         $service = gwmi win32_service -ComputerName $server -filter "displayname='$Srv'" -Credential $cred
  24.         $service.Change($null, $null, $null, $null, $null, $null, $ServiceAccount, $ServicePassword).ReturnValue
  25.         # Status
  26.         if ($service.Change().ReturnValue -eq "0")
  27.             {
  28.             Write-Host "Logon successfully Changed" -ForegroundColor "Green"
  29.             }
  30.         ELSE
  31.             {
  32.             Write-Host "Have a look for Status" $service.Change().ReturnValue "at https://msdn.microsoft.com/en-us/library/aa393673(v=vs.85).aspx" -ForegroundColor "Red"
  33.             }
  34.         Start-Service -DisplayName $Srv
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement