1

与下面的代码一样,处理脚本大约需要一个小时。我正在尝试寻找减少时间的方法,因为该脚本将被安排为每天运行。

我注意到在创建新对象时,它会$_.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
4

2 回答 2

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

为避免重复测量对象更改为此:

$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
于 2014-07-02T00:31:03.707 回答
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

    }
于 2014-07-02T20:26:12.993 回答