Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

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

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 modify to be able to do the same for ZIP files. Working in the same manner, report back any ZIP files which contain the words set out but also any files within the ZIP...

Any help?

"`n" 
write-Host "Search Running" -ForegroundColor Red
$filePath = "\fileservermydeptsIT"
"`n" 

Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*test*" -or $_.Name -like "*bingo*" -or $_.Name -like "*false*" -or $_.Name -like "*one two*" -or $_.Name -like "*england*") } | Select-Object Name,Directory,CreationTime,LastAccessTime,LastWriteTime | Export-Csv "C:scriptssearchescsv
esults.csv" -notype

write-Host "------------END of Result--------------------" -ForegroundColor Green
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:patho
eport.csv
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...