Windows PowerShell and Services

To find all the cmdlets that are related to Windows services, execute the following command:

Get-Command -CommandType cmdlet *service*

 

Get-Service

To find all the running services that start with SQL by executing the following command:

Get-Service -DisplayName "SQL*" -Computername RemoteServerName | Where-Object {$_.status -eq "Running"}

 

To see all the services whose name begins with ‘‘SQL’’ but exclude any service name that has the word ‘‘Writer’’ from the output:

Get-Service -Include "SQL*" -Exclude "*Writer*" -ComputerName RemoteServerName | Where-Object {$_.status -eq "Running"}

 

To list all the properties or methods associated with the Get-Service cmdlet:

Get-Service | Get-Member -MemberType properties

Get-Service | Get-Member -MemberType methods

 

To display the status of the Windows service "aspnet_state" and then try to start and then stop the service:

(Get-Service -Name "aspnet_state").status
(Get-Service -Name "aspnet_state").start()
(Get-Service -Name "aspnet_state").status
Start-Sleep 5
(Get-Service -Name "aspnet_state").status
(Get-Service -Name "aspnet_state").stop()
(Get-Service -Name "aspnet_state").status
Start-Sleep 5
(Get-Service -Name "aspnet_state").status

 

Stop-Service

To show the status of the service, stop the service, and then show the status of the service again:

Get-Service -Name aspnet_state
Stop-Service -Name aspnet_state
Get-Service -Name aspnet_state

 

To stop all the SQL Server–related services on the local machine and preview the results:

Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Running"} | Stop-Service -WhatIf

 

To stop all the SQL Server–related services on the local machine one by one with confirmation:

Get-Service -Displayname *sql* | Where-Object {$_.Status -eq "Running"}| Stop-Service -confirm

or   Stop-Service -Displayname *sql* -Confirm

 

Start-Service

To start the SQLAgent$SQL2008 service:

Start-Service -Name SQLAgent`$SQL2008
Get-Service -Name SQLAgent`$SQL2008

 

to start all the SQL Server–related services that are stopped with a WhatIf or confirm:

Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Stopped"} | Start-Service -WhatIf

Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Stopped"} | Start-Service -confirm

 

Set-Service

 

To use Set-Service to change the startup type from Manual to Automatic:

Set-Service -Name "SQLAGENT`$SQL2008" -StartupType "Automatic"

 

Note that you cannot stop and start a service on a remote machine using Stop-Service and Start-Service yet. The -computername option may become available in the official Windows PowerShell 2.0 RTM release.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值