与下面的代码一样,处理脚本大约需要一个小时。我正在尝试寻找减少时间的方法,因为该脚本将被安排为每天运行。
我注意到在创建新对象时,它会$_.Tag.Substring
为每个标签使用相同的 1440 次,并且有超过 200 个标签。
此外,使用 Measure-Object 3 次处理相同的 $_.Group 以获得最小值、平均值和最大值。
我如何以一种不会重复执行相同流程的方式设置它们?
$data = Get-ChildItem $destinationPath\PPP_*.csv |
ForEach-Object { $_.FullName } |
Import-Csv -Header Tag, Value, Date, Time, Mode, Alarm
$data | ForEach-Object {
[pscustomobject]@{
"Column 1" = $_.Tag.Substring(8,2)
"Pump Unit" = $_.Tag.Substring(10,2)
"Column 3" = $_.Tag.Substring(12,2)
"Column 4" = $_.Tag.Substring(14,1)
"Value" = $_.Value
"Time" = $_.Time
}
} | Export-Csv -Path "$destinationPath\Summary.csv" -NoTypeInformation
$data | Group-Object Tag | ForEach-Object {
[pscustomobject]@{
"Column 1" = $_.Name.Substring(8,2)
"Pump Unit" = $_.Name.Substring(10,2)
"Column 3" = $_.Name.Substring(12,2)
"Column 4" = $_.Name.Substring(14,1)
"Minimum" = ($_.Group | Measure-Object -Property Value -Minimum).Minimum
"Average" = ($_.Group | Measure-Object -Property Value -Average).Average
"Maximum" = ($_.Group | Measure-Object -Property Value -Maximum).Maximum
}
} | Export-Csv -Path "$destinationPath\Statistics.csv" -NoTypeInformation