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

3 years, 8 months ago.

  • Author
  • #29650

    Topics: 9
    Replies: 7
    Points: 0
    Rank: Member


    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

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

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    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

    Topics: 6
    Replies: 236
    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

    Topics: 9
    Replies: 7
    Points: 0
    Rank: Member

    Thanks, that helped me a lot

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

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort