我有这个脚本......
查看给定的网络位置,然后浏览所有文件夹/子文件夹以搜索特定的单词/短语。希望修改能够对ZIP文件执行相同操作。以相同的方式工作,报告任何包含所列单词的ZIP文件,以及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
答案 0 :(得分:2)
如果安装了.Net 4.5,则可以使用 System.IO.Compression.FileSystem 库创建一个打开并遍历zip文件内容的函数
$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中的完整路径,压缩大小等。
Get-ChildItem -Path -$filePath *.zip -Recurse -ErrorAction SilentlyContinue | % {
openZip $_.FullName >> c:\path\to\report.csv
}
答案 1 :(得分:0)
您无法扫描zip文件的内容,因为它已被压缩,并且只会像垃圾一样出现。您必须解压缩该文件,然后搜索它。
请按照以下说明操作:http://www.howtogeek.com/tips/how-to-extract-zip-files-using-powershell/
在本地复制,然后在那里搜索,然后删除副本,重复。