Powershell to search for files based on words / phrase but also Zip files and within zip files

后端 未结 2 1474
栀梦
栀梦 2020-12-21 19:26

I have this script...

Which looks at a given network location and then goes through all the folders / subfolders to search for specific words / phrases. Looking to m

相关标签:
2条回答
  • 2020-12-21 20:15

    If you have .Net 4.5 installed you can use System.IO.Compression.FileSystem library to create a function which will open and traverse contents of your zip files

    $searchTerms = @( "test","bingo","false","one two","england")
    function openZip($zipFile){
        try{
            [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null;
            $zipContens = [System.IO.Compression.ZipFile]::OpenRead($zipFile);  
            $zipContens.Entries | % {
                foreach($searchTerm in $searchTerms){
                    if ($_.Name -imatch $searchTerm){
                        Write-Output ($_.Name + "," + $_.FullName + "," + $_.CompressedLength + "," + $_.LastWriteTime + "," + $_.Length);
                    }
                }               
            }
        }
        catch{
            Write-Output ("There was an error:" + $_.Exception.Message);
        }
    }  
    

    You can then run something like this to obtain filename,full path within zip, compressed size etc.

    Get-ChildItem -Path -$filePath *.zip -Recurse -ErrorAction SilentlyContinue | %  {
        openZip $_.FullName  >> c:\path\to\report.csv
        }
    
    0 讨论(0)
  • 2020-12-21 20:27

    You cannot scan the contents of a zip file, since it is compressed and will just come out as garbage. You would have to unzip the file and then search through it.

    Follow these instructions: http://www.howtogeek.com/tips/how-to-extract-zip-files-using-powershell/

    Copy it down locally and then search for it there, then delete the copies, repeat.

    0 讨论(0)
提交回复
热议问题