Filtering two services at the same time.

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Helder Guzman Helder Guzman 1 year, 10 months ago.

  • Author
  • #29650
    Profile photo of Helder Guzman
    Helder Guzman


    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 ?

  • #29652
    Profile photo of TomBaker

    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
  • #29672
    Profile photo of Rob Simmers
    Rob Simmers

    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


  • #29679
    Profile photo of Bob McCoy
    Bob McCoy

    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
  • #29706
    Profile photo of Helder Guzman
    Helder Guzman

    Thanks, that helped me a lot

You must be logged in to reply to this topic.