Author Posts

July 9, 2017 at 4:25 pm

Hi,
Wrote a script which will search through *.bat & *.ps1 file and provide the output.

$Results = Foreach ($Server in (get-content "C:\Users\temp\Desktop\Test\Search_String\Server.txt"))
{get-childitem \\$Server\c$\test\ -recurse -Include *.bat, *.ps1 | SELECT-STRING -pattern '$4ret&yrt5' -SimpleMatch | Select @{Name="Server";Expression={$Server}},path,Linenumber
}
$Results | Select Server,path,Linenumber | Export-Csv C:\Users\temp\Desktop\Test\Search_String\string_match_results.csv -NoType

I am getting output which has string in ".bat" files. But not able to get output for ".PS1" files.
Can someone help me, where I am doing wrong.

-Kalyan

July 9, 2017 at 5:02 pm

Did you confirm that there are *.ps1 files in the given location? Did you confirm that there are some occurences of the searched pattern in the files? Did you confirm that your Get-ChildItem delivers the output you expect?
For the future: Please format your code as code and consider to indent your code and try not to use aliasses or abbreviations. This way would it be much easier to read and understand and of course to debug the code.

$ServerList = get-content 'C:\Users\temp\Desktop\Test\Search_String\Server.txt'
$Results = Foreach ($Server in $ServerList){
    Get-ChildItem '\\$Server\c$\test\*' -Recurse -Include *.bat, *.ps1 | 
        Select-String -Pattern '$4ret&yrt5' -SimpleMatch | 
            Select-Object @{Name='Server';Expression={$Server}},Path,Linenumber
}
$Results | 
    Select Server,path,Linenumber | 
        Export-Csv C:\Users\temp\Desktop\Test\Search_String\string_match_results.csv -NoTypeInformation