mikedopp icon

ChangeServiceAccount.ps1

mikedopp | PRO | 09/22/17 06:31:41 PM UTC | 0 ⭐ | 363 👁️ | Never ⏰ | []
PowerShell |

1.46 KB

|

None

|

0 👍

/

0 👎

clear 
# New Service Account
$ServiceAccount = "<Username>"  
$ServicePassword = "<Password>" 
 
# Array of Services which should be changed, Use the real "Display Name" of each Service
$Srvs = ('Sophos Agent', 'Adobe Acrobat Update Service');
 
#Prompt for an administartiv user name and password 
$Cred = Get-Credential
  
#Read the servers listed in the server.txt file located in the script folder
$servers = Get-Content "C:\Powershell\server.txt"
foreach ($server in $servers)
    {
    # Stop, Configure and Restart of the listed Services above
    foreach ($Srv in $Srvs) 
        {
        $gsrv = Get-Service -DisplayName $Srv
        Write-Host "Service $Srv on $server" -foreground "Green";
        Stop-Service -DisplayName $gsrv -Force
        Set-Service -InputObject $gsrv -StartupType Automatic
        $service = gwmi win32_service -ComputerName $server -filter "displayname='$Srv'" -Credential $cred
        $service.Change($null, $null, $null, $null, $null, $null, $ServiceAccount, $ServicePassword).ReturnValue
        # Status
        if ($service.Change().ReturnValue -eq "0") 
            {
            Write-Host "Logon successfully Changed" -ForegroundColor "Green"
            }
        ELSE
            {
            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"
            }
        Start-Service -DisplayName $Srv
        }
    }

Comments