Powershell Script to Change SQL Service Account username and password on Local and Remote Computers

$UserName = “SQL\Administrator”
$Password = “Password123”
$Service = “MSSQLSERVER” #Change your own service name
$Agent = “SQLSERVERAGENT” #Change your own service name
$computer = “Node2” #Change your own server/computer name ( Example “Node2″,”Node3″,”Node4” )
#Prompt you for user name and password to access remote computer
$Cred = Get-Credential
$svc_Obj= Get-WmiObject Win32_Service -ComputerName $computer -filter “name=’$service'” -Credential $cred
$StopStatus = $svc_Obj.StopService()
If ($StopStatus.ReturnValue -eq “0”)
{Write-host “The service ‘$Service’ Stopped successfully in $computer”}
$ChangeStatus = $svc_Obj.change($null,$null,$null,$null,$null,
$null,$UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq “0”)
{Write-host “User Name sucessfully for the service ‘$Service’ in $computer”}
$StartStatus = $svc_Obj.StartService()
If ($ChangeStatus.ReturnValue -eq “0”)
{Write-host “The service ‘$Service’ Started successfully in $computer”}

$svc_Obj1= Get-WmiObject Win32_Service -ComputerName $computer -filter “name=’$Agent'” -Credential $cred
$StopStatus = $svc_Obj1.StopService()
If ($StopStatus.ReturnValue -eq “0”)
{Write-host “The service ‘$Agent’ Stopped successfully in $computer”}
$ChangeStatus = $svc_Obj1.change($null,$null,$null,$null,$null,
$null,$UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq “0”)
{Write-host “User Name sucessfully for the Agent ‘$Agent’ in $computer”}
$StartStatus = $svc_Obj1.StartService()
If ($ChangeStatus.ReturnValue -eq “0”)
{Write-host “The service ‘$Agent’ Started successfully in $computer”}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.