与下面的代码一样,处理脚本大约需要一个小时。我正试图找到减少时间的方法,因为这个脚本将被安排每天运行。
我注意到在创建新对象时,每个标记使用相同的$_.Tag.Substring
1440次,并且有200多个标记。
此外,Measure-Object使用3次来处理相同的$ _。组以获得最小值,平均值和最大值。
如何以不再重复执行相同流程的方式设置它们?
$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
答案 0 :(得分:3)
试试这个。我认为这需要PowerShell v3或更高版本才能将Add-Member生成的属性“粘贴”到$ data元素。
$data = $data | ForEach-Object {
Add-Member -InputObject $_ NoteProperty -Name "Column 1" -Value ($_.Tag.Substring(8,2))
Add-Member -InputObject $_ NoteProperty -Name "Pump Unit" -Value ($_.Tag.Substring(10,2))
Add-Member -InputObject $_ NoteProperty -Name "Column 3" -Value ($_.Tag.Substring(12,2))
Add-Member -InputObject $_ NoteProperty -Name "Column 4" -Value ($_.Tag.Substring(14,1))
$_
}
$data | ForEach-Object {
[pscustomobject]@{
"Column 1" = $_."Column 1"
"Pump Unit" = $_."Pump Unit"
"Column 3" = $_."Column 3"
"Column 4" = $_."Column 4"
"Value" = $_.Value
"Time" = $_.Time
}
} | Export-Csv -Path "$destinationPath\Summary.csv" -NoTypeInformation
为避免重复Measure-Object更改:
$data | Group-Object Tag | ForEach-Object {
$measInfo = $_.Group | Measure-Object -Property Value -Minimum -Maximum -Average
[pscustomobject]@{
"Column 1" = $_.Group[0]."Column 1"
"Pump Unit" = $_.Group[0]."Pump Unit"
"Column 3" = $_.Group[0]."Column 3"
"Column 4" = $_.Group[0]."Column 4"
"Minimum" = $measInfo.Minimum
"Average" = $measInfo.Average
"Maximum" = $measInfo.Maximum
}
} | Export-Csv -Path "$destinationPath\Statistics.csv" -NoTypeInformation
答案 1 :(得分:0)
我从Keith的代码中修改了一下,以便所有内容都来自一个ForEach-Object
:
Get-ChildItem $destinationPath\PPP_*.csv |
ForEach-Object { $_.FullName } |
Import-Csv -Header Tag, Value, Date, Time, Mode, Alarm |
Group-Object -Property Tag |
ForEach-Object {
$tag = $_.Name
$column1 = $tag.Substring(8,2)
$pumpUnit = $tag.Substring(10,2)
$column3 = $tag.Substring(12,2)
$column4 = $tag.Substring(14,1)
$_.Group | ForEach-Object {
[pscustomobject]@{
"Column 1" = $column1
"Pump Unit" = $pumpUnit
"Column 3" = $column3
"Column 4" = $column4
"Value" = $_.Value
"Time" = $_.Time
}
} | Export-Csv -Path "$destinationPath\Summary.csv" -Append -NoTypeInformation
$measInfo = $_.Group | Measure-Object -Property Value -Minimum -Average -Maximum
[pscustomobject]@{
"Column 1" = $column1
"Pump Unit" = $pumpUnit
"Column 3" = $column3
"Column 4" = $column4
"Minimum" = $measInfo.Minimum
"Average" = $measInfo.Average
"Maximum" = $measInfo.Maximum
} | Export-Csv -Path "$destinationPath\Statistics.csv" -Append -NoTypeInformation
}