Link to home
Start Free TrialLog in
Avatar of John Z
John ZFlag for United States of America

asked on

Filtering Get-WmiObject in PowerShell with WQL filter

I'm having a problem properly filtering out PartComponent from Win32_SystemServices. I'm trying to determine if 'ClusSvc' is installed or not. If I filter another WMI Class, like Win32_ComputerSystem I can successfully perform the filter. Here's an example:

PS C:\Windows\system32> Get-WmiObject -Class Win32_ComputerSystem -Filter "Name LIKE '%'"

Domain              : mydomain.org
Manufacturer        : VMware, Inc.
Model               : VMware Virtual Platform
Name                : MyComputerName
PrimaryOwnerName    : Information Systems
TotalPhysicalMemory : 4294430720

Open in new window


However, when I try basically the same filter on Win32_SystemServices it fails:

PS C:\Windows\system32> Get-WmiObject -Class Win32_SystemServices -Filter "PartComponent LIKE '%'"
Get-WmiObject : Invalid query "select * from Win32_SystemServices where PartComponent LIKE '%'"
At line:1 char:1
+ Get-WmiObject -Class Win32_SystemServices -Filter "PartComponent LIKE '%'"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Open in new window


The difference of course is that Win32_ComputerSystem only ever returns one result for Name and PartComponent in Win32_SystemServices returns a big list of services. Anyone know how I can craft my filter to look inside that list and only return the one that contains 'ClusSvc'?

I know I could get the same result using the pipeline like this:
Get-WmiObject -ClassName "Win32_SystemServices" | Select-Object PartComponent | Where-Object {$_ -like "*ClusSvc*"}

Open in new window


However, I have found that is is often magnitudes quicker to use -Filter than to use the pipeline to select and sort and since I will be looking through many servers off of a list in my script I'm looking to maximize that performance with this query. Feel free to tell me I'm crazy. Thank you ahead of time.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Z

ASKER

Excellent, I was just reading about linked classes and was going to start looking at which classes Win32_SystemServices linked, but you've done that work for me!

Unfortunately, I'm stuck with Get-WmiObject due to PS v2 restrictions on some of the servers that I will be querying. Thanks for the help though!
You can still use Get-CimInstance (since it converts dates for you), but you have to swap its transport back to DCOM when acting against server which only run PS 2 (or don't have remoting enabled).
$cimSession = New-CimSession ComputerName -SessionOption (New-CimSessionOption -Protocol DCOM)
Get-CimInstance Win32_Service -Filter "Name LIKE '%ClusSvc%'" -CimSession $cimSession

Open in new window