Home > Net >  Powershell Select property with a variable as
Powershell Select property with a variable as

Time:10-02

The following script searches deletes statements in SSIS. These delete statements came from sql cache for some reason they came back with "]" missing. This missing brace is causing my select object to fail see error below. I cant remove braces before search else it will not find matches but leaving it on is causing issues with selection. Is there a way to ignore these braces during the select object. If the delete does not have any braces the script works like a charm.

$SearchStrings = Get-Content "C:\Users\someuser\Desktop\DataDumps_PS\Delete_input.txt"
$out_file = "C:\Users\someuser\Desktop\DataDumps_PS\Deletes$(get-date -f yyyyMMdd).txt"

$RootString = "E:\SSIS_packages\"
Foreach($SearchString in $SearchStrings){
Write-host $SearchString
                                        
Get-ChildItem $RootString -recurse | Select-String -pattern $SearchString | group path | 
Select-Object name,@{name="SearchString";expression={$SearchString.replace('[','').replace(']','')}}  | Export-Csv $out_file - 
append -NoTypeInformation -Force #select name, "$SearchString"
}

Error for deletes that have a missing brace.

Select-String : The string DELETE FROM [dbo].[APP_DMEVS_STG is not a valid regular 
expression: parsing "DELETE FROM [dbo].[APP_DMEVS_STG" - Unterminated [] set.

CodePudding user response:

Sounds like it's failing on interpreting it as a regular expression. Try replacing this

Select-String -pattern $SearchString

with this

Select-String -SimpleMatch "$SearchString"
  • Related