0

I am really trying to speed this script up. I have a directory with about 17k files in it:

$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
           }
Mochael_DLite
  • 167
  • 2
  • 5
  • 14
  • How long does this part take? ``select-string -path $path -pattern $pattern`` because you do it twice. You could set that to a variable in one line, count it the next, and export it to a CSV in the next line. – Chris N Jan 20 '16 at 19:41
  • it took almost 2 hours to complete and hammered my CPU. I have to get the count so that it knows to send the email. I couldn't figure out a way to do all in one – Mochael_DLite Jan 20 '16 at 19:43
  • I posted an answer on how to not run that same command twice.. secondly, look into a utility that's more specialized for this task and see if it's any faster. findstr should be in your path Windows – Chris N Jan 20 '16 at 19:52

1 Answers1

1

If Select-String is taking a long time in the script above, try only doing it once. Also, I see that you check the count and if nothing is going on, you delete that CSV you made. So howabout you count it first, and then only make it if you need to.

...

$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...
}
Chris N
  • 7,239
  • 1
  • 24
  • 27