Filtering two services at the same time.

Welcome Forums General PowerShell Q&A Filtering two services at the same time.

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
3 years, 1 month ago.

  • Author
    Posts
  • #29650

    Participant
    Points: 0
    Rank: Member

    HI

    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

    Participant
    Points: 0
    Rank: Member

    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 {$_.name -in "tlmagent", "OpswareAgent"} | select PSComputerName,Name,DisplayName,StartMode,Status,@{Label='LogonAs';Expression={$_.StartName}}  | ConvertTo-Html -Fragment
    
  • #29672

    Participant
    Points: 0
    Rank: Member

    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
    

    Another

  • #29679

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Thanks, that helped me a lot

The topic ‘Filtering two services at the same time.’ is closed to new replies.