繁体   English   中英

powershell-在新的属性中合并数组属性

[英]powershell - combine array properties in new one

我有一些带有很多行(数以千计)的数组。 它具有“列XXXXX ”列

行示例:

Group_name = "proxy_users"
Column1   = "domain\Igor"
Column2   = null
.......
Column989 = 'domain\Andrew'
Column999 = 'domain\Mike'

创建新变量(忽略“空”值将成为“ ColumnXXX”的总和)的正确和快速方法是什么?

就像“域\\ igor,域\\ Andrew,域\\ mike”

我可以像$ group一样使用smth | 选择-Property“ Column *” ...但是如何求和以及如何忽略null?

您可以使用ex列出所有属性。 $_.psobject.properties ,过滤出所需的内容,然后使用-join组合值。 防爆

$o = [pscustomobject]@{
    Group_Name = "Test"
    Column1 = "Foo"
    Column2 = $null
    Column3 = "1"
}, [pscustomobject]@{
    Group_Name = "Test2"
    Column1 = $null
    Column2 = "Bar"
    Column3 = "2"
}

#Add property as constant
$o | ForEach-Object {
    $_ | Add-Member -NotePropertyName Summed -NotePropertyValue (($_.psobject.Properties | Where-Object { $_.Name -ne 'Group_name' -and $_.Value -ne 'null' } | Select-Object -ExpandProperty Value) -join '' )
}
$o | ft

或者您可以使用ScriptProperty来计算每次调用的值

#Remember to exclude itself to avoid infinite recursion
$o | Add-Member -MemberType ScriptProperty -Name Summed -Value {($this.psobject.Properties | Where-Object { $_.Name -ne 'Group_name' -and $_.Name -ne 'Summed' -and $_.Value -ne 'null' } | Select-Object -ExpandProperty Value) -join '' }
$o | ft

结果:

Group_Name Column1 Column2 Column3 Summed
---------- ------- ------- ------- ------
Test       Foo             1       Foo1
Test2              Bar     2       Bar2

或者,可以将$_.Name -like 'Column*' -and $_.Value -ne 'null' ColumnXXX $_.Name -like 'Column*' -and $_.Value -ne 'null' ColumnXXX $_.Name -like 'Column*' -and $_.Value -ne 'null'用作属性过滤器(如果它们实际上被称为ColumnXXX

用更简洁的PSv4 +版本来补充Frode F.基于优雅的ScriptProperty的解决方案 ,该版本由于避免了在属性定义脚本块中使用cmdlet( 管道 ),从而使操作员 (具有大量的输入对象,这可能很重要):

$o | Add-Member -PassThru -MemberType ScriptProperty -Name Joined -Value { 
 @($this.psobject.Properties.Where({$_.Name -match 'Column*'}).Value) -ne 'null' -join ', ' 
}

注意以下用途:

  • .Where()集合运算符(PSv4 +),是Where-Object cmdlet的更快替代方法。

  • 成员枚举 (PSv3 +),在集合级别访问属性会生成元素的属性值的数组
    例如, $this.psobject.Properties.Name产生$this.psobject.Properties集合的所有元素的.Name属性值。

  • 将比较运算符-ne应用于数组值的 LHS,在这种情况下,该运算符充当过滤器 :将该运算符应用于每个元素 ,然后将匹配元素作为数组返回; 注意LHS周围的@(...) ,即使将其仅返回单个值,也可确保将其视为数组。

使用问题中的样本数据,以上结果(查找属性Joined ):

Group_name : proxy_users
Column1    : domain\Igor
Column2    : null
Column989  : domain\Andrew
Column999  : domain\Mike
Joined     : domain\Igor, domain\Andrew, domain\Mike

通过上述优化,您甚至可以考虑使用一个更简单的Select-Object解决方案,该解决方案构造包含输入对象属性( * )的自定义对象实例,以及包含感兴趣的组合列值的新计算属性:

$o | Select-Object *, @{ n='Joined'; e={ 
  @(($_.psobject.Properties.Where({$_.Name -match 'Column*'})).Value) -ne 'null' -join ', ' } 
}

此解决方案比上面的解决方案慢,但幅度不大。
需要注意的是,如果您将所有输出收集在内存中,则新构造的对象除了原始对象外还会占用空间。


可选阅读:性能比较; 管道使用的影响:

底部的代码采用了Frode和我的回答的各种方法。

这是我机器上的一个示例计时-绝对数字并不重要,但是它们的比率是(尽管我不知道CPU核心数量和磁盘速度等因素会如何发挥作用)-输入集大小为1000对象:

Approach                                 TotalSeconds
--------                                 ------------
add-member w/ scriptproperty - operators 0.35
select-object w/ noteproperty            0.40
add-member w/ scriptproperty - pipeline  0.72
add-member w/ noteproperty - pipeline    0.98

结论:

  • 在属性定义中使用管道的解决方案的速度明显慢了约2倍,即使输入数量较大,该解决方案也似乎没有变化。

  • 创建对象(而不是向输入对象添加属性)的基于优化Select-Object的解决方案仅比优化Add-Member解决方案稍慢。

测试的源代码:

# The number of input objects to test with.
# Adjust to experiment.
$objCount = 1000

Write-Verbose -Verbose "# of input objects: $objCount"

$ndx = 0; $approaches = 'select-object w/ noteproperty', 
                        'add-member w/ scriptproperty - operators', 
                        'add-member w/ scriptproperty - pipeline', 
                        'add-member w/ noteproperty - pipeline'
$tempFile = New-TemporaryFile # requires PSv5+
$(foreach($approach in $approaches) {

  # Create test collection (recreate it in every iteration to support repeated Add-Member operations)
  $al = [System.Collections.ArrayList]::new()
  for ($i = 0; $i -lt $objCount; ++$i) {
    $null = $al.add([pscustomobject] @{ one = 1; Column1 = 'col1'; Column2 = 'col2'; Column3 = 'null'; Column4 = 'col4' })
  }

  Measure-Command {

    Write-Verbose -Verbose "Running: $approach..."
    switch ($ndx) {
      0 { # select-object w/ noteproperty
        $al | Select-Object *, @{ n='Joined'; e={ @(($_.psobject.Properties.Where({ $_.Name -match 'Column*'})).Value) -ne 'null' -join ', ' } } | 
          Export-Csv $tempFile  
        break
      }

      1 { # add-member w/ scriptproperty - operators
        $al | Add-Member -PassThru -MemberType ScriptProperty -Name Joined -Value { @($this.psobject.Properties.Where({ $_.Name -match 'Column*'}).Value) -ne 'null' -join ', ' } |
          Export-Csv $tempFile          
        break
      }

      2 { # add-member w/ scriptproperty - pipeline
        $al | Add-Member -PassThru -MemberType ScriptProperty -Name Joined -Value { ($this.psobject.Properties | Where-Object { $_.Name -match 'Column*'  -and $_.Value -ne 'null' } | Select-Object -ExpandProperty Value) -join ', ' }  | 
         Export-Csv $tempFile
         break
        }

        3 { # add-member w/ noteproperty - pipeline; requires an intermediate ForEach-Object call
          $al | ForEach-Object {
            $_ | Add-Member -PassThru -NotePropertyName Joined -NotePropertyValue (($_.psobject.Properties | Where-Object { $_.Name -match 'Column*' -and $_.Value -ne 'null' } | Select-Object -ExpandProperty Value) -join ', ' )
          } |
            Export-Csv $tempFile
        break
      }
      default { Throw "What are you doing here?" }
    }

    # Import-Csv $tempFile | Select-Object -First 1 Joined | Write-Verbose -Verbose

    ++$ndx

  } | Select-Object @{ n='Approach'; e={ $approach }}, @{ n='TotalSeconds'; e={ '{0:N2}' -f $_.TotalSeconds } }

}) | Sort-Object { [double] $_.TotalSeconds }

Remove-Item $tempFile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM