In this exercise, we will explore the use of the Get-WmiObject cmdlet as we retrieve user information from WMI.
Start Windows PowerShell by using Start | Run | PowerShell.
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
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...
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
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
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
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...
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
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
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"}
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
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"}
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"}
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
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
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
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
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.
This concludes this step-by-step exercise. If you have any problems with any of the commands in this exercise, refer to the StepByStep.txt file in the scripts folder for this chapter.