Sethu's blog

dev@sqlservr.microsoft

Change Service Account Password for Sql Server / SQL Agent Accounts

Change Service Account Password for Sql Server / SQL Agent Accounts

  • Comments 1
changeSvcAcct.ps1 ###########################################################################################
# Scenario:Change  Service account  for SQL Server, Agent services
#
#  How to use this powershell script:
#    - Launch SQL Server PowerShell ( Start -> Run -> sqlps.exe)
#    - Copy the following powershell script and paste it in powershell window
###########################################################################################

# Change Service Account Password for Sql Server / SQL Agent  Accounts
# note: if needed, this helper can be extended to AS, RS services
function ChangeServiceAccount
{
        Param([string]$machineName = $(Read-Host "MachineName") ,
                [string]$instanceName = $(Read-Host "Instance name(Ex: DEFAULT / SQLINST1)") ,
                [string]$serviceType = $(Read-Host "Service Type (Ex:SqlServer / SqlAgent)"),
                [string]$userAccountName = $(Read-Host "Account Name (Ex:Domain\user)"),
                [string]$password = $(Read-Host "Password:")
        )

       if([String]::IsNullOrEmpty($machineName))
       {
                write-error "Machine name is not valid"
                return
       }

        if([String]::IsNullOrEmpty($instanceName))
       {
                write-error "instance name is not valid"
                return
       }
       
       $serviceName = ""
       
       if($serviceType -eq "SqlServer" )
       {
                if($instanceName -eq "DEFAULT")
                {
                        $serviceName  = "MSSQLSERVER"
                }
                else
                {
                        $serviceName  = "MSSQL$" + $instanceName       
                }
       }
       
       if($serviceType -eq "SqlAgent" )
       {
                if($instanceName -eq "DEFAULT")
                {
                        $serviceName  = "SQLSERVERAGENT"
                }
                else
                {
                        $serviceName  = "SqlAgent$" + $instanceName     
                }
       }
       
        $PSPath = "\SQL\" + $machineName
        
        $machine = get-item $PSPath
        
        write-host "Changing service account for"
        write-host "SQL Instance:"$machineName"\"$instanceName
        write-host "Service type: " $serviceType
        write-host "User account:"$userAccountName
        write-host "Service Name:"$serviceName
        
        ## DevNote: - using a key that has "$" does not work as expected
        ## $serviceObject= $machine.ManagedComputer.Services[$serviceName]
        ## following is the workaround
       
        foreach($svc in $machine.ManagedComputer.Services)
        {
                if($svc.Name -eq $serviceName)
                {
                        $serviceObject = $svc
                        break
                }
        }
       
        $serviceObject.SetServiceAccount($userAccountName, $password)
       
        write-host "Account changed!"
}


ChangeServiceAccount  

# This posting is provided "AS IS" with no warranties, and confers no rights.
# Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Comments
  • Hi Sethu, will this script restart SQL Server Services ?

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post