Select-字符串用时太长。我该如何优化它
Select-String is taking too long. How can I optimize it
我真的想加快这个脚本的速度。我有一个包含大约 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
}
如果 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...
}
我真的想加快这个脚本的速度。我有一个包含大约 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
}
如果 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...
}