Using -like to find strings with Powershell - regex

In Powershell, I need to find multiple errors within a text file and the closest desired word between them. Each error is contained in an array. In the past, I would use the code:
#creating null array
$results = #("")
#creating index for array
for ($i = 0; $i -lt ($errors.length - 1); $i++)
{
$results += $false
}
#selecting string
for ($i = 0; $i -lt $errors.length; $i++)
{
$k = $errors[$i]
$rg = [regex]"WORD.*?$k.*?WORD"
$results[$i] = $content | Select-String -Pattern $rg -AllMatches | Foreach-Object {($_.Matches |
ForEach-Object {$_.value})}
}
$errors is the array of errors, $content is the content of the text file, and each item in $results holds the string from desired word to error to desired word.
Using $results[$i] = $content | Select-String -Pattern $rg -AllMatches | Foreach-Object {($_.Matches | ForEach-Object {$_.value})} does not work because my errors contain wild cards like asterisks.
I know that in order for me to do such characters, I need to use -like
I have tried using $results[$i] = $content -like $k instead, but that only returns a null value.

You can try:
for ($i = 0; $i -lt $errors.length; $i++)
{
$k = [regex]::Escape($errors[$i])
$rg = [regex]"(?s)WORD.*?$k.*?WORD"
$results[$i] = $content | Select-String -Pattern $rg -AllMatches |
Foreach-Object { $_.Matches.Value }
}
I do not know what is inside of $errors, $results, or $content. For what you want to work as intended while $content spans multiple lines, then $content will need to be a single string before being piped into Select-String. Otherwise, your regex will only match against one line at a time.
Since you may want .*? to match multiple lines, you should use the (?s) mode so that . can match new line characters.
If $errors contains characters that have special meaning in regex, you will need to backslash escape them. An easy way to do that is to call the Regex .NET class method Escape().

Related

PowerShell to match multiple lines with regex pattern

I write a Powershell script and regex to search two configs text files to find matches for Management Vlan. For example, each text file has two Management vlan configured as below:
Config1.txt
123 MGMT_123_VLAN
234 MGMT_VLAN_234
Config2.txt
890 MGMT_VLAN_890
125 MGMT_VLAN_USERS
Below is my script. It has several problems.
First, if I ran the script with the $Mgmt_vlan = Select-String -Path $File -Pattern $String -AllMatches then the screen output shows the expected four (4) Mgmt vlan, but in the CSV file output shows as follow
Filename Mgmt_vlan
Config1.txt System.Object[]
Config2.txt System.Object[]
I ran the script the output on the console screen shows exactly four (4) Management vlans that I expected, but in the CSV file it did not. It shows only these vlans
Second, if I ran the script with $Mgmt_vlan = Select-String -Path $File -Pattern $String | Select -First 1
Then the CSV shows as follows:
Filename Mgmt_vlan
Config1.txt 123 MGMT_123_VLAN
Config2.txt 890 MGMT_VLAN_890
The second method Select -First 1 appears to select only the first match in the file. I tried to change it to Select -First 2 and then CSV shows column Mgmt_Vlan as System.Object[].
The result output to the screen shows exactly four(4) Mgmt Vlans as expected.
$folder = "c:\config_folder"
$files = Get-childitem $folder\*.txt
Function find_management_vlan($Text)
{
$Vlan = #()
foreach($file in files) {
Mgmt_Vlan = Select-String -Path $File -Pattern $Text -AllMatches
if($Mgmt_Vlan) # if there is a match
{
$Vlan += New-Object -PSObject -Property #{'Filename' = $File; 'Mgmt_vlan' = $Mgmt_vlan}
$Vlan | Select 'Filename', 'Mgmt_vlan' | export-csv C:\documents\Mgmt_vlan.csv
$Mgmt_Vlan # test to see if it shows correct matches on screen and yes it did
}
else
{
$Vlan += New-Object -PSObject -Property #{'Filename' = $File; 'Mgmt_vlan' = "Mgmt Vlan Not Found"}
$Vlan | Select 'Filename', 'Mgmt_vlan' | Export-CSV C:\Documents\Mgmt_vlan.csv
}
}
}
find_management_vlan "^\d{1,3}\s.MGMT_"
Regex correction
First of all, there are a lot of mistakes in this code.
So this is probably not code that you actually used.
Secondly, that pattern will not match your strings, because if you use "^\d{1,3}\s.MGMT_" you will match 1-3 numbers, any whitespace character (equal to [\r\n\t\f\v ]), any character (except for line terminators) and MGMT_ chars and anything after that. So not really what you want. So in your case you can use for example this: ^\d{1,3}\sMGMT_ or with \s+ for more than one match.
Code Correction
Now back to your code... You create array $Vlan, that's ok.
After that, you tried to get all strings (in your case 2 strings from every file in your directory) and you create PSObject with two complex objects. One is FileInfo from System.IO and second one is an array of strings (String[]) from System. Inside the Export-Csv function .ToString() is called on every property of the object being processed. If you call .ToString() on an array (i.e. Mgmt_vlan) you will get "System.Object[]", as per default implementation. So you must have a collection of "flat" objects if you want to make a csv from it.
Second big mistake is creating a function with more than one responsibility. In your case your function is responsible for gathering data and after that for exporting data. That's a big no no. So repair your code and move that Export somewhere else. You can use for example something like this (i used get-content, because I like it more, but you can use whatever you want to get your string collection.
function Get-ManagementVlans($pattern, $files)
{
$Vlans = #()
foreach ($file in $files)
{
$matches = (Get-Content $file.FullName -Encoding UTF8).Where({$_ -imatch $pattern})
if ($matches)
{
$Vlans += $matches | % { New-Object -TypeName PSObject -Property #{'Filename' = $File; 'Mgmt_vlan' = $_.Trim()} }
}
else
{
$Vlans += New-Object -TypeName PSObject -Property #{'Filename' = $File; 'Mgmt_vlan' = "Mgmt Vlan Not Found"}
}
}
return $Vlans
}
function Export-ManagementVlans($path, $data)
{
#do something...
$data | Select Filename,Mgmt_vlan | Export-Csv "$path\Mgmt_vlan.csv" -Encoding UTF8 -NoTypeInformation
}
$folder = "C:\temp\soHelp"
$files = dir "$folder\*.txt"
$Vlans = Get-ManagementVlans -pattern "^\d{1,3}\sMGMT_" -files $files
$Vlans
Export-ManagementVlans -path $folder -data $Vlans```
Summary
But in my opinion in this case is overprogramming to create something like you did. You can easily do it in oneliner (but you didn't have information if the file doesn't include anything). The power of powershell is this:
$pattern = "^\d{1,3}\s+MGMT_"
$path = "C:\temp\soHelp\"
dir $path -Filter *.txt -File | Get-Content -Encoding UTF8 | ? {$_ -imatch $pattern} | select #{l="FileName";e={$_.PSChildName}},#{l="Mgmt_vlan";e={$_}} | Export-Csv -Path "$path\Report.csv" -Encoding UTF8 -NoTypeInformation
or with Select-String:
dir $path -Filter *.txt -File | Select-String -Pattern $pattern -AllMatches | select FileName,#{l="Mgmt_vlan";e={$_.Line}} | Export-Csv -Path "$path\Report.csv" -Encoding UTF8 -NoTypeInformation

Select all backslashes between two chars

I am working on a powershell script and I've got several text files where I need to replace backslashes in lines which matches this pattern: .. >\\%name% .. < .. (.. could be anything)
Example string from one of the files where the backslashes should match:
<Tag>\\%name%\TST$\Program\1.0\000\Program.msi</Tag>
Example string from one of the files where the backslashes should not match:
<Tag>/i /L*V "%TST%\filename.log" /quiet /norestart</Tag>
So far I've managed to select every char between >\\%name% and < with this expression (Regex101):
(?<=>\\\\%name%)(.*)(?=<)
but I failed to select only the backslashes.
Is there a solution which I could not yet find?
I'd recommend selecting the relevant tags with an XPath expression and then do the replacement on the text body of the selected nodes.
$xml.SelectNodes('//Tag[substring(., 1, 8) = "\\%name%"]' | ForEach-Object {
$_.'#text' = $_.'#text' -replace '\\', '\\'
}
So here's my solution:
$original_file = $Filepath
$destination_file = $Filepath + ".new"
Get-Content -Path $original_file | ForEach-Object {
$line = $_
if ($line -match '(?<=>\\\\%name%)(.*)(?=<)'){
$line = $line -replace '\\','/'
}
$line
} | Set-Content -Path $destination_file
Remove-Item $original_file
Rename-Item $destination_file.ToString() $original_file.ToString()
So this will replace every \ with an / in the given pattern but not in the way which my question was about.

regex in powershell - not change three characters before text

Is there any easy way to do this?
input: 123215-85_01_test
expected output: 01_test
Another example
input: 12154_02_test
expected output: 02_test
There will be always string "test", but different numbering before
for example this code..
$path = "c:\tmp\*.sql"
get-childitem $path | forEach-object {
$name = $_.Name
$result = $name -replace "","" # I don't know how write this regex..
$extension = $_.Extension
$newName = $prefix+"_"+ $result -f, $extension
Rename-Item -Path $_.FullName -NewName $newName
}
There are two ways you go go at this. Simple split and join or you can use one of many regexes....
Split on underscore and rejoin last 2 elements
$split = "123215-85_01_test" -split "_"
$split[-2..-1] -join "_" # $split[-2,-1] would also work.
Regex to locate the data between the last underscores
"123215-85_01_test" -replace "^.*_(\d+)_(.*)$", '$1_$2'
Note this fails if there is more than 2 underscores.

Replace different occurences of String with different values in powershell?

I am pretty new to powershell scripting.The scenario is that I have to replace the first occurrence of a string with different value and second occurrence with a different value.
So far, I have this :
$dbS = Select-String $repoPath\AcceptanceTests\sample.config -Pattern([regex]'dbServer = "#DB_SERVER#"')
write-output $dbS[0]
write-output $dbS[1]
This gives the output as :
D:\hg\default\AcceptanceTests\sample.config:5: dbServer = "#DB_SERVER#"
D:\hg\default\AcceptanceTests\sample.config:12: dbServer = "#DB_SERVER#"
I can see that both the occurrences are correct, and this returns a MatchInfo object.Now I need to replace the contents,I tried :
Get-Content $file | ForEach-Object { $_ -replace "dbserver",$dbS[0] } | Set-Content ($file+".tmp")
Remove-Item $file
Rename-Item ($file+".tmp") $file
But this replaces all occurence and that too with the entire path. Please help..
Here is what i have come up with:
$dbs = Select-String .\test.config -pattern([regex]'dbServer = "Test1"')
$file = Get-Content .\test.config
$dbs | % {$file[$_.linenumber-1] = $file[$_.linenumber-1] -replace "Test1", "Test3" }
set-content .\test.config $file
It cycles through all results of Select-String and uses its .LineNumber Property (-1) as array index to replace the text only in that line. Next we just set the content again.
If you want to assign different Values for occurance 1 and 2 you can do this:
#replace first occurance
$file[$dbs[0].LineNumber-1] = $file[$dbs[0].LineNumber-1] -replace "Test1", "Test2"
#replace second occurance
$file[$dbs[1].LineNumber-1] = $file[$dbs[1].LineNumber-1] -replace "Test1", "Test3"
This approach obviously only works if you know how many occurances you will have and which of them you want to replace.

Powershell Lines of code count

Im having some trouble counting the lines of code in all my powershell projects.
I want to ignore comment sections in my count, unfortunately I am not that good with regular expressions.
So what I want to achieve is to exclude all the "Synopsis help code in functions"
<#
.SYNOPSIS
#>
And my own comment blocks
<#
Get-ADUser -Identity ThisUserDoesNotExist
ThisCodeIsCommentedOut
#>
What I have so far is
Get-Content Script.ps1 | ?{$_ -ne "" -and $_ -notlike "#*"}
If you are in v3.0 I suggest to use this script: http://poshcode.org/4789
Here the relevant part modified just to count lines of code of a script file:
$file = ".\My_Script_File.ps1"
$fileContentsArray = Get-Content -Path $file
if ($fileContentsArray)
{
$codeLines = $null
$tokenAst = $null
$parseErrorsAst = $null
# Use the PowerShell 3 file parser to create the scriptblock AST, tokens and error collections
$scriptBlockAst = [System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$tokenAst, [ref]$parseErrorsAst)
# Calculate the 'lines of code': any line not containing comment or commentblock and not an empty or whitespace line.
# Remove comment tokens from the tokenAst, remove all double newlines and count all the newlines (minus 1)
$prevTokenIsNewline = $false
$codeLines = #($tokenAst | select -ExpandProperty Kind | where { $_ -ne "comment" } | where {
if ($_ -ne "NewLine" -or (!$prevTokenIsNewline))
{
$_
}
$prevTokenIsNewline = ($_ -eq "NewLine")
} | where { $_ -eq "NewLine" }).Length-1
$codeLines
}