0

我真的想加快这个脚本的速度。我有一个目录,里面有大约 17k 个文件:

$date= Get-Date -Format yyyyMMdd
$dir= "C:\test\$date"
$path=Get-ChildItem -Path $dir -Recurse
$pattern = "<RESULT>FAILED</RESULT>"
$submitted = (select-string -path $path -pattern $pattern | measure-object).Count
select-string -path $path -pattern $pattern | select Path,Filename,Line | Export-Csv -Path "D:\Failed.csv"

if($submitted -eq 0) {
    Remove-Item "D:\Failed.csv" -recurse
}
else
           {
    Send-MailMessage -From "noreply@email.com" -To users@email.com -Subject "Failed Report" -Body "Attached are the failed files.  This is for the last 3 hours.  There may be files that were already reported." -Attachments "D:\Failed.csv" -SmtpServer 0.0.0.0
    Remove-Item "D:\Failed.csv" -recurse
           }
4

1 回答 1

1

如果 Select-String 在上面的脚本中花费了很长时间,请尝试只执行一次。另外,我看到您检查了计数,如果没有发生任何事情,则删除您制作的 CSV。所以,你先数数,然后只在需要时才算。

...

$submitted = select-string -path $path -pattern $pattern
...
if(($submitted | Measure-Object).Count -gt 0){
   ...make the csv using the $submitted variable as the source...
   ...send the csv...
   ...delete the csv...
}
于 2016-01-20T19:46:42.987 回答