# ============================================================================================== # # NAME: ManageSQLServerService.ps1 # # AUTHOR: Yan Pan # DATE : 9/11/2007 # # COMMENT: This script stops a SQL Server service, changes its service account, # and then restart the service. # ============================================================================================== # . for the local computer # If you want to connect to a remote machine, specify the machine name here. $strComputer = "." # mydomain\SqlService is the new service account, and "password" is its password. # You should replace both before you run this script in your environment. $strUserName = "mydomain\SqlService" $strPassword= "password" # MSSQLSERVER for default instance. # Use a filter like "ServiceName='MSSQL`$Instance1' for named instance. $sqlservice = Get-WmiObject ­computerName $strComputer ­namespace root\Microsoft\SqlServer\ComputerManagement -class SqlService ­filter "ServiceName='MSSQLSERVER'" $sqlservice.SetServiceAccount($strUserName, $strPassword) $sqlservice.StopService() $sqlservice.StartService()