希望有人能提出建议来帮助我加速Powershell脚本。我正在做的是读取数百个CSV文件,解析信息以获取有关缺失条目的数据,然后将该输出写入HTML文件。这是我用来处理文件的循环:
ForEach ($Filename in $FileList) {
$CustTemp = import-csv "$FilePath\$Filename"
$CustName = $CustTemp[0].CustName
Write-Host "Reading data for $CustName"`r
For ($counter=0;$counter -lt 31;$counter++){
$CheckDate = (get-date).AddDays(-$counter)
$CheckShortDate = $CheckDate.ToShortDateString()
$TempData = import-csv "$FilePath\$Filename" | Select FileName,FileDate | where {$_.FileDate -eq $CheckShortDate}
If ($TempData -eq $null) {
$row = "No file found for $CheckShortDate for $CustName"
$HTMLReportItems += $row
}
$HTMLReportItems = $HTMLReportItems | ConvertTo-Html -Fragment
}
}
当我使用一些CSV文件进行测试时,这个循环工作正常但是当针对大量文件(300+)运行时,循环对于每个文件(30s-1m)来说需要很长时间才能完成。我很确定为什么每次迭代都要访问CSV文件30次。我希望有人会对如何处理数据提出更好的建议。
答案 0 :(得分:1)
您多次阅读$FilePath\$Filename
。在for
循环之外读取它,只在里面进行过滤。将HTML生成移动到循环外部。
$HTMLReportItems = foreach ($Filename in $FileList) {
$csv = Import-Csv (Join-Path $FilePath $Filename)
$CustName = $csv[0].CustName
$data = $csv | select FileName,FileDate
Write-Host "Reading data for $CustName"
for ($counter=0;$counter -lt 31;$counter++){
$CheckShortDate = (Get-Date).AddDays(-$counter).ToShortDateString()
$TempData = $data | ? {$_.FileDate -eq $CheckShortDate}
if ($TempData -eq $null) {
"No file found for $CheckShortDate for $CustName"
}
}
}
$HTMLReportItems = $HTMLReportItems | ConvertTo-Html -Fragment