Obtaining Service Information: Step-by-Step Exercises


In this exercise, we will explore the use of the Get-WmiObject cmdlet as we retrieve user information from WMI.

  1. Start Windows PowerShell by using Start | Run | PowerShell.

  2. From the Windows PowerShell prompt, use the Get-Service cmdlet to obtain a listing of all the services and their associated status. The command to do this is Get-Service. This is shown here:

     Get-Service

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

     Status   Name               DisplayName ------   ----               ----------- Running  Alerter            Alerter Running  ALG                Application Layer Gateway Service Stopped  AppMgmt            Application Management Stopped  aspnet_state       ASP.NET State Service Running  AudioSrv           Windows Audio Running  BITS               Background Intelligent Transfer Ser...

  4. Use the Sort-Object cmdlet to sort the listing of services. Specify the status property for the Sort-Object. To do this, pipeline the results of the Get-Service 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:

     Get-Service |sort -property status

  5. A partial output from this command is shown here:

     Status   Name               DisplayName ------   ----               ----------- Stopped  RasAuto            Remote Access Auto Connection Manager Stopped  RDSessMgr          Remote Desktop Help Session Manager Stopped  odserv             Microsoft Office Diagnostics Service Stopped  ose                Office Source Engine

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

     Get-Service |sort -property name

  7. A partial output of this command is shown here:

     Status   Name               DisplayName ------   ----               ----------- Running  Alerter            Alerter Running  ALG                Application Layer Gateway Service Stopped  AppMgmt            Application Management Stopped  aspnet_state       ASP.NET State Service Running  AudioSrv           Windows Audio Running  BITS               Background Intelligent Transfer Ser...

  8. Use the Get-Service cmdlet to produce a listing of services. Sort the object returned by both the name and the status of the service. The command to do this is shown here:

     Get-Service |sort status, name

  9. A partial output of this command is shown here:

     Status   Name               DisplayName ------   ----               ----------- Stopped  AppMgmt            Application Management Stopped  aspnet_state       ASP.NET State Service Stopped  Browser            Computer Browser Stopped  CcmExec            SMS Agent Host Stopped  CiSvc              Indexing Service

  10. Use the Get-Service cmdlet to return an object containing service information. Pipeline the resulting object in to a Where-Object cmdlet. Look for the word server in the display name. The resulting command is shown here:

     Get-Service | where {$_.DisplayName -match "server"}

  11. The resulting listing is shown here:

     Status   Name               DisplayName ------   ----               ----------- Running  DcomLaunch         DCOM Server Process Launcher Running  InoRPC             eTrust Antivirus RPC Server Running  InoRT              eTrust Antivirus Realtime Server Running  InoTask            eTrust Antivirus Job Server Stopped  lanmanserver       Server Stopped  MSSQL$SQLEXPRESS   SQL Server (SQLEXPRESS) Stopped  MSSQLServerADHe... SQL Server Active Directory Helper Stopped  SQLBrowser         SQL Server Browser Stopped  SQLWriter          SQL Server VSS Writer

  12. Use the Get-Service cmdlet to retrieve a listing of service objects. Pipeline the resulting object to the Where-Object. Use the equals argument to return an object that represents the Alerter service. The code that does this is shown here:

     Get-Service | where {$_.name -eq "alerter"}

  13. Use the up arrow to retrieve the previous command that retrieves the Alerter service. Store the resulting object in a variable called $a. This code is shown here:

     $a=Get-Service | where {$_.name -eq "alerter"}

  14. Pipeline the object contained in the $a variable into the Get-Member cmdlet. You can use the gm alias to simplify typing. This code is shown here:

     $a | gm

  15. Using the object contained in the $a variable, obtain the status of the Alerter service. The code that does this is shown here:

     $a.status

  16. If the Alerter service is running, then stop it. To do so, use the Stop-Service cmdlet. Instead of pipelining the object in the $a variable, we use the -inputobject argument from the Stop-Service cmdlet. The code to do this is shown here:

     Stop-Service -InputObject $a

  17. If the Alerter service was stopped, then use the Start-Service cmdlet instead of the Stop-Service cmdlet. Use the -inputobject argument to supply the object contained in the $a variable to the cmdlet. This is shown here:

     Start-Service -InputObject $a

  18. Query the status property of the object contained in the $a variable to confirm that the Alerter services’ status changed. This is shown here:

     $a.status

    If you are working with a service that has its Startup Type set to Disabled, then PowerShell will not be able to start it and will return an error.

  19. This concludes this step-by-step exercise. If you have any problems with any of the commands in this exercise, refer to the image from book StepByStep.txt file in the scripts folder for this chapter.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

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