0

I am running a test on servers with the following line:

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter "State='Running'" |
where-object ??? }| Foreach-Object {
                New-Object -TypeName PSObject -Property @{
                    DisplayName=$_.DisplayName
                    State=$_.State
                } | Select-Object DisplayName,State
            # Export all info to CSV
            } | ft -AutoSize

I would like to create a variable like this:

$IgnoreServices = '"Wireless Configuration","Telephony","Secondary Logon"

and send this to Where-Object. Can I do this?

Sune:)

EDIT: After some R/T (research and trying:)) I found out that I can do this:

$IgnoreServices = {$_.DisplayName -ne "Wireless Configuration" 
-and $_.DisplayName -ne "Telephony" -and $_.DisplayName -ne "Secondary Logon" 
-and $_.DisplayName -ne "Windows Event Collector"}

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter   "State='Running'"|        where-object $IgnoreServices | Foreach-Object {
                # Set new objects for info gathered with WMI
                New-Object -TypeName PSObject -Property @{
                    DisplayName=$_.DisplayName
                    State=$_.State
                } | Select-Object DisplayName,State
            # Export all info to CSV
            } | ft -AutoSize

But.. I would REALLY like if one could specify services to be excluded in the following manner: "service1","service2","service3"

As always, all help is greatly appreciated!!

1
  • Just a quick tip: you don't need the "select." The output of select-object is also a psobject and since you're selecting all of the source objects properties, it is a redundant operation. Commented Dec 31, 2011 at 17:53

2 Answers 2

5

Yeah you can just do:

$IgnoreServices = "Wireless Configuration","Telephony","Secondary Logon"

like you wanted and do the following in the where-object:

where-object { $IgnoreServices -notcontains $_.DisplayName  }
Sign up to request clarification or add additional context in comments.

1 Comment

You ninja!! Works like a charm!! Thank you very much!!
2

You can do that with a WMI filter (runs faster), and since you only select properties there's no need to create new objects, use the Select-Object cmdlet instaed:

$filter = "State='Running' AND Name <> 'Wireless Configuration' AND Name <> 'Telephony' AND Name <> 'Secondary Logon'"

Get-WmiObject Win32_Service -ComputerName myserver -Filter $filter | Select-Object DisplayName,State

5 Comments

Thank you Shay and @manojlds ! I'll remove new object (I use that in another get-wmi and didn't know I didn't need it.) My list of "blacklisted" services turned out be quite extensive.. How would I go about reading the list from a textfile?
This will build the filter dynamically assuming you have a text file with a service name on each line: $svc = Get-Content .\svc.txt | Foreach-Object { "AND Name <> '{0}'" -f $_}; $filter = "State='Running' " + ($svc -join ' ')
How would I use this in my get-wmi query? I've tried directly and by calling it with -filter $svc to no avail..
Nevermind, I just had a brainfart. Works excellent!! Thank you very much!
This approach will perform better since the filter is being performed during the initial WMI query and Powershell doesn't have to compare the DisplayName of every object returned from the query against the list of ignored services. The performance improvement is negligible when querying just one computer but can be come noticeable when scaling up. Using measure-command in my test showed the WMI filter approach is 27% faster. The filtered WMI query took 255 milliseconds while the where-object method took 348.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.