Managing Services


PowerShell offers several cmdlets that make managing Windows servers a breeze. The cmdlets you will use most often are briefly discussed in the following sections.

Listing Services

We've used the Get-Service cmdlet in many examples throughout this book. Here's a standard expression to list all running services:

 PS C:\> get-service | Where {$_.status -eq "Running"} 

This is an important expression because you need to know either the service's real name or its displayname to manage the service.

Starting Services

It should come as no surprise that PowerShell uses the Start-Service cmdlet to start a service. You can specify the service by its real name:

 PS C:\> start-service -name "spooler" 

Alternatively, you can specify the service by its display name:

 PS C:\> start-service -Displayname "Print spooler" 

Be sure to use quotes for the displayname since it usually contains spaces. Because the cmdlet doesn't display a result unless there is an error, you might want to combine two service management cmdlets in one expression:

 PS C:\> start-service -displayname "print spooler" ; ` >> get-service -displayname "print spooler" >> Status   Name               DisplayName ------   ----               ----------- Running  Spooler            Print Spooler PS C:\> 

Now there's no confusion as to the state of the Print Spooler service after the Start-Service cmdlet.

Finally, here's a code snippet you might use to stop a service that is running:

 PS C:\> $svc=get-service webclient PS C:\> if ($svc.status -eq "running") { >> stop-service webclient ; get-service webclient >> } >> Status   Name               DisplayName ------   ----               ----------- Stopped  WebClient          WebClient PS C:\> 

The snippet checks the status of the WebClient service. If the status equals Running, then the service is stopped and re-queried. If you need to restart a service, use the Restart-Service cmdlet using the same parameters as Start-Service.

Stopping Services

Stopping services is just as easy. Everything we discussed about starting services applies to stopping services. The only difference is we use the Stop-Service cmdlet.

 PS C:\> stop-service webclient ; get-service webclient Status   Name               DisplayName ------   ----               ----------- Stopped  WebClient          WebClient PS C:\> 

Some services can be suspended or paused. This doesn't stop the service completely; it only prevents it from doing anything. Be aware that not all services support suspension:

 PS C:\> suspend-service spooler Suspend-Service : Service 'Print Spooler (Spooler)' cannot be suspended because the service does not support being suspended or resumed. At line:1 char:16 + suspend-service <<<< spooler PS C:\> 

If you can suspend a service, you'll see something like this:

 PS C:\> suspend-service w3svc ; get-service w3svc Status   Name               DisplayName ------   ----               ----------- Paused   W3SVC              World Wide Web Publishing PS C:\> 

Use the Resume-Service cmdlet to resume a suspended service:

 PS C:\> resume-service w3svc ; get-service w3svc Status   Name               DisplayName ------   ----               ----------- Running  W3SVC              World Wide Web Publishing PS C:\> 

The Set-Service cmdlet is used to change a service's start mode, say from Auto to Manual. You can either specify the service by name or displayname.

 PS C:\> set-service -name spooler -StartupType Manual 

You can change the Startup Type to Automatic, Manual, or Disabled. Unfortunately, PowerShell has no method for changing the service account or its password.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net