Asked by:
where-object that returns objects that match criteria and use a string array as exclusion criteria

Question
-
The script I am working on will Return matching driver objects.
i.e.
$driverMatch = Get-CimInstance Win32_PnPSignedDriver | Select-Object ClassGuid, DeviceName, DriverVersion, InfName | Where-Object {$_.ClassGuid -eq "$ClassGUID" -and $_.DeviceName -like "$DisplayName"}
The script user will have three variables to use as input: The generic search query to match the driver description ($DisplayName); the device class guid ($ClassGUID); and the part I'm having a problem with an array of exclude strings ($ExcludeDisplayName)
I my head I am getting a "set of objects that match my first criteria; ClassGUID and DisplayName. Then I am going a step further, checking the DeviceName of those objects for a match again my exclude strings and if found throwing those objects out. This would in the end leave me with a set of objects that match the classGUID and the generic display name and do not have any of the exclude strings included in their name.
[string]$DisplayName = "*USB*" # Search Query to Match Driver Description. Be sure to include * (wildcards) where appropriate [string]$ClassGUID = "{36fc9e60-c465-11cf-8056-444553540000}" # DeviceID - Use the following; Device Manager \ Device \ Details Tab \ Device class guid Property [String[]]$ExcludeDisplayName = "*Generic*","*Root*" $driverMatch = Get-CimInstance Win32_PnPSignedDriver | Select-Object ClassGuid, DeviceName, DriverVersion, InfName | Where-Object {$_.ClassGuid -eq "$ClassGUID" -and $_.DeviceName -like "$DisplayName"} | Where-Object {$ExcludeDisplayName -notcontains $_.DeviceName}
The problem is the second Where-Object \ filter doesn't work. It isn't filtering out the objects that match the exclude wildcards
- Edited by thebman313 Tuesday, January 30, 2018 8:17 PM
- Moved by Bill_Stewart Monday, March 12, 2018 9:07 PM Abandoned
Tuesday, January 30, 2018 4:11 PM
All replies
-
You are making a lot of statements about what you are doing. You have not asked a question or posted an error. What is it that you are asking us to answer?
\_(ツ)_/
Tuesday, January 30, 2018 7:58 PM -
You are making a lot of statements about what you are doing. You have not asked a question or posted an error. What is it that you are asking us to answer?
\_(ツ)_/
Tuesday, January 30, 2018 8:17 PM -
What where object is an exclude?
This will only ever return one object or no objects:
Get-CimInstance Win32_PnPSignedDriver | Select-Object ClassGuid, DeviceName, DriverVersion, InfName | Where-Object { $_.ClassGuid -eq $ClassGUID -and $_.DeviceName -like $DisplayName }
Which is correctly written as:
Get-CimInstance Win32_PnPSignedDriver -Filter "ClassGuid = '$ClassGUID' AND DisplayName LIKE '$DisplayName '" Select-Object ClassGuid, DeviceName, DriverVersion, InfName
I see no exclusions.
\_(ツ)_/
Tuesday, January 30, 2018 8:25 PM -
Note also the PnPDriver does not have a "DisplayName" property. Perhaps you mean "DeviceID"?
You need to analyze the class properties and decide what it is you are trying to do.
\_(ツ)_/
- Edited by jrv Tuesday, January 30, 2018 8:37 PM
Tuesday, January 30, 2018 8:36 PM -
So I have an array of words that I would like to use as excludes
[String[]]$ExcludeDisplayName = "*Generic*","*Root*"
If the Display name has any of those words in it I don't want to return that object. Please see the last pipe at the end of the following code.
$driverMatch = Get-CimInstance Win32_PnPSignedDriver | Select-Object ClassGuid, DeviceName, DriverVersion, InfName | Where-Object {$_.ClassGuid -eq "$ClassGUID" -and $_.DeviceName -like "$DisplayName"} | Where-Object {$ExcludeDisplayName -notcontains $_.DeviceName}
Tuesday, January 30, 2018 8:41 PM -
As noted there is no "DisplayName' property. Use either DevieID or DeviceName
$exclude = 'Generic|Root' Get-CimInstance Win32_PnPSignedDriver | Where{$_.DeviceName -notmatch $exclude}
\_(ツ)_/
Tuesday, January 30, 2018 8:46 PM -
For what you are doing this is all you need:
Get-CimInstance Win32_PnPSignedDriver -filter "DeviceClass='USB'"| Select-Object ClassGuid, DeviceName, DriverVersion, InfName
\_(ツ)_/
Tuesday, January 30, 2018 8:52 PM