0

The value of $DeviceID is : "PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\3&11583659&1&B0"

I'm trying to search for that string in .INF files with "Select-String" :

 Select-String -Path C:\file.inf -Pattern "$DeviceID"

But it won't take the string as is, it's having a problem with the "\V":

Select-String : La chaîne PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\
3&11583659&1&B0 n’est pas une expression régulière valide: analyse de
"PCI\VEN_8086&DEV_9D3A&SUBSYS_225617AA&REV_21\3&11583659&1&B0" - Séquence
d'échappement \V non reconnue.
Au caractère Ligne:15 : 5
+     Select-String -Path $($_.FullName) -pattern "$($erreur.DeviceID)"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument : (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand

Sorry for the french but it basically says "String is not a valid regex. Escape sequence \V not recognized".

Rakha
  • 1,874
  • 3
  • 26
  • 60

3 Answers3

1

Select-String, by default, uses the .NET regex engine. To do simple string matching, use the -SimpleMatch switch parameter:

Select-String -Path C:\file.inf -pattern "$DeviceID" -SimpleMatch
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks, forgot about that switch! Added a further question about regex if you can chime in on that too. – Rakha May 29 '19 at 12:01
  • 1
    @Rakha `$null = $id -match "VEN_\w{4}&DEV_\w{4}"` (for future reference, please post a new question rather than updating the existing question if you have more/new/followup/different questions) – Mathias R. Jessen May 29 '19 at 14:02
1

PowerShell is trying to do a regular expression match. Add the -SimpleMatch switch to look for the literal string in $DeviceID, without regex.

Select-String -Path C:\file.inf -Pattern $DeviceID -SimpleMatch
Jay Adams
  • 2,052
  • 1
  • 12
  • 7
  • Thanks, forgot about that switch! Added a further question about regex if you can chime in on that too. – Rakha May 29 '19 at 12:56
1

I see you already have your answer but there is another switch that will do this as well.

For comparison...

-list

select-string -path "$TargeUNC\*.ps1" -Pattern 'Get-WmiObject' -list | 
Select-Object -First 3

# Results

2018-01-15 Enable the Disk Cleanup tool on Windows Server.ps1:45:$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
3D_chart.ps1:1:get-wmiobject win32_perfformatteddata_perfdisk_logicaldisk
7 cmdlet Hyper-V Tips.ps1:15:$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class 

-vs -SimpleMatch

select-string -path "$TargeUNC\*.ps1" -Pattern 'Get-WmiObject' -SimpleMatch | 
Select-Object -First 3

# Results

2018-01-15 Enable the Disk Cleanup tool on Windows Server.ps1:45:$wmiOS = Get-WmiObject -Class Win32_OperatingSystem
3D_chart.ps1:1:get-wmiobject win32_perfformatteddata_perfdisk_logicaldisk
7 cmdlet Hyper-V Tips.ps1:15:$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class 
postanote
  • 15,138
  • 2
  • 14
  • 25