A common mistake with WMI/CIM filters is:
PS> Get-WmiObject -Class Win32_LogicalDisk -Filter “DeviceId=C:”
Get-WmiObject : Invalid query “select * from Win32_LogicalDisk where DeviceId=C:”
At line:1 char:1
+ Get-WmiObject -Class Win32_LogicalDisk -Filter “DeviceId=C:”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCo
mmand
The clue is in the invalid query error message
When you use the –Filter parameter and are testing a property of type string the value you are testing against has to be in quotes
PS> Get-WmiObject -Class Win32_LogicalDisk -Filter “DeviceId=’C:'”
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 108999528448
Size : 248480002048
VolumeName : Windows
The filter is defined as a string so you need to use single quotes inside the double quotes. You could mess around with all single quotes but then you have to escape the inner set of quotes – good luck with that – its an unnecessary exercise in frustration
How do you know the property is a string?
PS> (Get-CimClass -ClassName Win32_LogicalDisk).CimClassProperties[‘DeviceId’]
Name : DeviceID
Value :
CimType : String
Flags : Property, Key, ReadOnly, NullValue
Qualifiers : {CIM_Key, read, key, MappingStrings…}
ReferenceClassName :
The same rules for –Filter apply to Get-CimInstance
Get-CimInstance -ClassName Win32_LogicalDisk -Filter “DeviceId=’C:'”
The post WMI Filters appeared first on PowerShell for Windows Admins.