Powershell 可根据单词/短语以及 Zip 文件和 zip 文件搜索文件 [英] Powershell to search for files based on words / phrase but also Zip files and within zip files

查看:57
本文介绍了Powershell 可根据单词/短语以及 Zip 文件和 zip 文件搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本...

查看给定的网络位置,然后遍历所有文件夹/子文件夹以搜索特定的单词/短语.希望修改以能够对 ZIP 文件执行相同的操作.以同样的方式工作,报告任何包含所列出的单词的 ZIP 文件以及 ZIP 中的任何文件...

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...

有什么帮助吗?

"`n" 
write-Host "Search Running" -ForegroundColor Red
$filePath = "\\fileserver\mydepts\IT"
"`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:\scripts\searches\csv\results.csv" -notype

write-Host "------------END of Result--------------------" -ForegroundColor Green

推荐答案

如果您安装了 .Net 4.5,您可以使用 System.IO.Compression.FileSystem 库来创建一个函数,该函数将打开和遍历 zip 文件的内容

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);
    }
}  

然后您可以运行类似这样的程序来获取文件名、zip 中的完整路径、压缩大小等.

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
    }

这篇关于Powershell 可根据单词/短语以及 Zip 文件和 zip 文件搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆