John Z
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:
However, when I try basically the same filter on Win32_SystemServices it fails:
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:
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.
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
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
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*"}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
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!