Databases Reference
In-Depth Information
You can start and stop a Windows service using the methods available for
Get
-
Service
. The following
example displays the status of the Windows service
"aspnet_state"
and then tries to start and then stop
the service (see Figure 7-8):
(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
Figure 7-8
From the preceding snapshot, you can see that we got the status of the service using properties, and
started and stopped the service using the
start()
and
stop()
methods, respectively. You can also see the
status changing from ''
StartPending
'' and ''
StopPending
'' to ''
Running
'' and ''
Stopped
'', respectively.
Stop-Service
Instead of using the methods from
Get
-
Service
to stop a service, you could use the cmdlet
Stop
-
Service
.
Assume that you want to stop the aspnet state service using the
stop-service
cmdlet. The following
example shows the status of the service, stops the service, and then shows the status of the service again
(see Figure 7-9).
Get-Service -Name aspnet_state
Stop-Service -Name aspnet_state
Get-Service -Name aspnet_state
Now assume that you want to stop all the SQL Server-related services on the local machine. You can use
the
Get
-
Service
and
Stop
-
Service
cmdlets together. The following steps lead to stopping all running
SQL-related services. It would be a disaster if you ran the
Stop-Service
cmdlet on a production machine,
so you can use the -
WhatIf
switch parameter with the
Stop-Service
cmdlet and preview the results (see
Figure 7-10):
Get-Service -DisplayName *sql* | Where-Object {$_.Status -eq "Running"} | Stop-
Service -WhatIf
Search WWH ::
Custom Search