Filtering two services at the same time.


Quick question

I need to filter two services using wmi. can you do it in one line like

Get-WmiObject -Computer $servername Win32_Service | Where-Object name (“tlmagent”, “OpswareAgent”)

and then convert to hml the output.

would you create a new psobject to write-output ?

If you can pipeline it, just use Select to grab the properties you want.
Use a hashtable with Label=“Title” and Expression={} to create custom columns

When combining data from multiple cmdlets, then I like to use a [pscustomobject] and populate the appropriate properties from each cmdlet.

Get-WmiObject -Computer $servername Win32_Service | Where-Object {$ -in "tlmagent", "OpswareAgent"} | select PSComputerName,Name,DisplayName,StartMode,Status,@{Label='LogonAs';Expression={$_.StartName}}  | ConvertTo-Html -Fragment

You should always filter as far left as possible in the pipeline. Your command is getting ALL services and then filtering. Consider:

$services = Get-WmiObject -Computer $servername Win32_Service -Filter "Name = 'tlmagent' or Name =  'OpswareAgent' "

$services | ConvertTo-HTML


Rob’s absolutely right about filtering early in the pipeline. In this case it means structuring your WQL query to get just what you want and nothing more. You will also want to use Select-Object so you are only displaying the properties you are interested in.

$servername = "myserver"
$services = Get-WmiObject -Computer $servername Win32_Service -Filter "Name = 'usosvc' or Name =  'spooler' "
$services | select Name, Status | ConvertTo-HTML | out-file -FilePath .\foo.htm
invoke-item -path .\foo.htm

Thanks, that helped me a lot