Jul 3 2011 5:06AM GMT
Posted by: Richard Siddaway
Operating System, WMI
Computer Report: II Service Information
Posted by: Richard Siddaway
The batch file retrieves service information using
wmic service where state="Running" get DisplayName, Caption
Direct PowerShell translation is this.
Get-WmiObject -Class Win32_Service -Filter "State=’Running’" | Select DisplayName, Caption
Couple of problems:
- DisplayName and Caption tend to show the same output
- What about services that aren’t running? A major problem could be resolved if you realised a service that should be running has stopped.
Lets change the WMI to
Get-WmiObject -Class Win32_Service | sort State | Select State, Name, Caption
When we create our function we can add a switch to restrict output to just running services if required
function get-servicestate{ [CmdletBinding()] param ( [string]$computer="localhost", [switch]$running ) BEGIN{}#begin PROCESS{ Write-Verbose "Get Services" $services = Get-WmiObject -Class Win32_Service -ComputerName $computer if ($running){ $services | where{$_.State -eq "Running"} | Select Name, Caption } else { $services | sort State | Select State, Name, Caption } }#process END{}#end }
By fetching all of the service information we allow ourselves the ability to easily modify the output if requirements change




