Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 242 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to ignore escape sequences stored in PowerShell string variable?

#1
In my PowerShell script, I'm running Select-String over a number of files, looking for a string passed into it via a variable ($id):

foreach ($file in (ls "path\to\files")) {
$found = $false
$found = Select-String -Path $file $id -Quiet
if ($found) {
break
}
}

Unfortunately, the $id variable sometimes things like "\C" or "\T", which Select-String tries to interpret as escape sequences. These are not valid escape sequences, so Select-String throws an error. They are not intended to be escape sequences (e.g., it could be part of a file path such as "C:\Test"), so how can I prevent PowerShell/Select-String from interpreting them as such? I've tried using parentheses around $id with no luck.
Reply

#2
If the $id string already contains something like TAB when it's passed to you then I'm not aware of a built in method to safely escape it back to "\t". You need to make sure your script is passed the correct string in the first place. I.e. it needs to passed 0x5C74 (\t) not 0x09 (TAB). So the escaping needs to be done when the the search string is first defined.

Regex.Escape will escape TAB -> \t but will also escape any of these characters that have meaning within regular expressions:

\, *, +, ?, |, {, [, (,), ^, $,., #, and white space

e.g.   . -> \\.

Reply

#3
Use the static `escape()` method, it instructs the regular expression engine to interpret these characters literally rather than as metacharacters:


$id = [regex]::escape($id)

You can also turn the command to a one liner (`-path` can take a collection of files):

Select-String -Path path\to\files\\* -Pattern ([regex]::escape($id)) -Quiet
Reply

#4
Select-String has a `-SimpleMatch` parameter that will cause the cmdlet to do simple string matches instead of regular expressions. If you change the script to do:

$found = Select-String -Path $file $id -Quiet -SimpleMatch

it should work as desired.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through