Not able to search string in PS1 files

Welcome Forums General PowerShell Q&A Not able to search string in PS1 files

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year, 4 months ago.

  • Author
    Posts
  • #74530

    Participant
    Points: 0
    Rank: Member

    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

  • #74534

    Participant
    Points: 134
    Helping Hand
    Rank: Participant

    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

The topic ‘Not able to search string in PS1 files’ is closed to new replies.