To get a list of services on the local or remote computer:
Get-WmiObject -class Win32_Service
Get-WmiObject –computerName RemoteHostName-class Win32_Service
To get the SQL Server service of the default instance on the local computer and its properties:
Get-WmiObject -class Win32_Service | Where-Object {$_.Name -eq 'MSSQLSERVER'} | Select-Object Name, ExitCode, ProcessId, StartMode, State, Status, Description,DisplayName, ErrorControl, PathName, Started, StartName, SystemName
The Win32 Service class also provides methods for working with the services:
StartService----Attempts to place a service into the startup state.
StopService----Places a service in the stopped state.
ChangeStartMode----Modifies the start mode of a service.
Change----Modifies a service.
GetSecurityDescriptor----Returns the security descriptor that controls access to the service. This method is available starting with Windows Vista.
SetSecurityDescriptor----Writes an updated version of the security descriptor that controls access to the service. This method is available starting with Windows Vista.
To start a stopped SQL Server–related service:
Get-WmiObject -class Win32_Service | Where-Object { ($_.Name -like '*SQL*') -and ($_.State -eq 'Stopped') }
(Get-WmiObject -class Win32_Service | Where-Object {$_.Name -eq 'SQLSERVERAGENT'}).StartService()
To verify that the SQLSERVERAGENT service started:
Get-WmiObject -class Win32_Service | Where-Object { ($_.Name -like 'SQLSERVERAGENT') }
To start the SQLSERVERAGENT service on the remote computer:
(Get-WmiObject -class Win32_Service –computerName RemoteHostName | Where-Object {($_.Name -eq 'SQLSERVERAGENT') }).StartService()
To stop all the running SQL Server–related services on the local computer:
Get-WmiObject -class Win32_Service | Where-Object { ($_.Name -like '*SQL*') -and ($_.State -eq 'Running') }
Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name -like '*SQL*') -and ($_.State -eq 'Running') } | Sort-Object Name -desc | ForEach-Object -process {$_.StopService(); Start-Sleep -s 15 }
To verify that all the SQL Server–related services have stopped:
Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name -like '*SQL*') -and ($_.State -eq 'Running')}
To change the start mode of the SQL Server Agent service of the default instance from Manual to Auto:
(Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name –like 'SQLSERVERAGENT') }).ChangeStartMode('Automatic')
## Possible values for the parameter include Boot, System, Automatic, Manual, and Disabled
To confirm that the StartMode has been changed.
Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name -like 'SQLSERVERAGENT') }
The method, Change, can be used to modify the start mode and other properties(display name, binary file path, error control, service account, loading order, and service dependencies) of a service,and to change the service account of the SQL Server:
# Gets the SQL Server service object
$service = Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name –eq 'MSSQLSERVER') }
# Gets the parameter collection of the Change method for the service object
$params = $service.psbase.GetMethodParameters('Change')
# Set the new service account
$params["StartName"] = [String] "LocalSystem"
# Apply the Change method to the SQL Server service object
$result= $service.psbase.InvokeMethod('Change', $params, $Null)
"The return code of the Change method is " + $result["ReturnValue"]
To verify that the service account has been changed, run the following command:
Get-WmiObject -class Win32_Service | Where-Object{ ($_.Name -like 'MSSQLSERVER') } | select StartName