Win32_Service

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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值