A question on the forum asking about filtering WMI results raises a number of interesting points.
The user wanted to pass a computername and a filter term to pull product information from remote machines. I ended up with this
$computername = $env:COMPUTERNAME
$filter = ‘Live’
$scriptblock = {
param($filter)
Get-WmiObject -Class Win32_product -Filter “Name LIKE ‘%$filter%'” |
Select IdentifyingNumber, Name, LocalPackage }
Invoke-Command -ComputerName $computername -ScriptBlock $scriptblock -ArgumentList $filter
You can pass an argument into the scriptblock you use with invoke-command by using the –Argumentlist parameter.
More interesting is the –Filter parameter on Get-Wmi-Object
-Filter “Name LIKE ‘%$filter%'”
Notice that % is the wildcard not * as you’d use for a string. Its always better to filter the results from Get-WmiObject using –Filter rather than a where-object after the call.
Of course you can just use the wmi or cim cmdlets directly for this problem which is even better
Get-WmiObject -Class Win32_Product -ComputerName $computername -Filter “Name LIKE ‘%$filter%'” | Select IdentifyingNumber, Name, LocalPackage
Get-CimInstance -ClassName Win32_Product -ComputerName $computername -Filter “Name LIKE ‘%$filter%'” | Select IdentifyingNumber, Name, LocalPackage