Windows PowerShell Step by Step by Ed Wilson

Windows PowerShell Step by Step by Ed Wilson

Author:Ed Wilson [Wilson, Ed]
Language: eng
Format: azw3, pdf
Publisher: Pearson Education
Published: 2015-10-13T04:00:00+00:00


* * *

Note

If you are working with a service that has its startup type set to Disabled, Windows PowerShell will not be able to start it and will return an error. If you do not have administrator rights, Windows PowerShell will be unable to stop the service.

* * *

In the following exercise, you will explore the use of the Win32_Service WMI class by using the Get-WmiObject cmdlet as you retrieve service information from your computer.

Using WMI for service information

1. Start the Windows PowerShell console.

2. From the Windows PowerShell prompt, use the Get-CimInstance cmdlet to obtain a listing of all the services and their associated statuses. Use the gcim alias instead of typing Get-CimInstance. The command to do this is shown here.

gcim win32_service

A partial listing of the output from this command is shown here.

Click here to view code image

ProcessId Name StartMode State Status ExitCode

--------- ---- --------- ----- ------ --------

0 AJRouter Manual Stopped OK 1077

0 ALG Manual Stopped OK 1077

0 AppIDSvc Manual Stopped OK 1077

940 Appinfo Manual Running OK 0

0 AppMgmt Manual Stopped OK 1077

0 AppReadiness Manual Stopped OK 1077

3. Use the Sort-Object cmdlet to sort the listing of services. Specify the state property for the Sort-Object cmdlet. To sort the service information based upon the state of the service, pipeline the results of the Get-CimInstance cmdlet into the Sort-Object cmdlet. Use the sort alias for the Sort-Object cmdlet to reduce the amount of typing. The results are shown here.

Click here to view code image

gcim win32_service | sort state

Partial output from this command is shown here.

Click here to view code image

ProcessId Name StartMode State Status ExitCode

--------- ---- --------- ----- ------ --------

1448 MpsSvc Auto Running OK 0

992 vmickvpexchange Manual Running OK 0

972 vmicrdv Manual Running OK 0

324 lmhosts Manual Running OK 0

656 LSM Auto Running OK 0

4. Use the Get-CimInstance cmdlet to produce a listing of services. Sort the resulting list of services alphabetically by DisplayName. To do this, use the Sort-Object cmdlet to sort the listing of services by the name property. Pipeline the object returned by the Get-CimInstance cmdlet into the Sort-Object cmdlet. The command to do this, using the sort alias for Sort-Object, is shown here.

Click here to view code image

gcim win32_service | sort DisplayName

Notice that the output does not appear to actually be sorted by the DisplayName property. There are two problems at work. The first is that there is a difference between the name property and the DisplayName property. The second problem is that the DisplayName property is not displayed by default. Partial output of this command appears here.

Click here to view code image

1448 BFE Auto Running OK 0

0 BDESVC Manual Stopped OK 1077

0 wbengine Manual Stopped OK 1077

0 bthserv Manual Stopped OK 1077

0 PeerDistSvc Manual Stopped OK 1077

940 CertPropSvc Manual Running OK 0

0 ClipSVC Manual Stopped OK 0

5. Produce a service listing that is sorted by DisplayName. This time, use the Select-Object cmdlet to display both the state and the DisplayName properties. Use the gcim, sort, and select aliases to reduce typing. The command appears here.

Click here



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.