PowerShell-用重复行扩展数组中的数组列 对于[pscustomobject]或[hashtable]实例的输入集合:对于[System.Data.DataRow]实例的输入集合:

我有一个名为System.Array的{​​{1}}对象,第一个([0])项如下所示:

$Data

我正试图取消整个数组上的第一列“ RecordDate” ,像这样:

RecordDate              : {43739,43740,43741,43742...}
MAX_LAST_UPDATE_DATE    : 30/10/2019 14:08:33
EMPLOYEE_NUMber         : 1000522
EFFECTIVE_START_DATE    : 01/10/2019 00:00:00
EFFECTIVE_END_DATE      : 31/12/4712 00:00:00
CC                      : 0726
REGION_NAME             : Head Office
LOCATION_NAME           : Inventory
FIRST_NAME              : Name
MIDDLE_NAMES            : Mid
LAST_NAME               : Last
KNOWN_AS                : NickName
JOB_TITLE               : Inventory Manager
WORK_NUMber             : 
Employment Category     : Full Time
NORMAL_HOURS            : 40
GROUP_NAME              : Indirect
Manager Employee Number : 1034422
PERSON_TYPE             : Employee
HIRE_DATE               : 16/11/1983 00:00:00
TERMINATION_DATE        : 
DATE_OF_BIRTH           : 23/05/1966 00:00:00
NATIONAL_IDENTIFIER     : 111

是否可以通过偷偷摸摸的RecordDate : 43739 MAX_LAST_UPDATE_DATE : 30/10/2019 14:08:33 EMPLOYEE_NUMber : 1000522 EFFECTIVE_START_DATE : 01/10/2019 00:00:00 EFFECTIVE_END_DATE : 31/12/4712 00:00:00 CC : 0726 REGION_NAME : Head Office LOCATION_NAME : Inventory FIRST_NAME : Name MIDDLE_NAMES : Mid LAST_NAME : Last KNOWN_AS : NickName JOB_TITLE : Inventory Manager WORK_NUMber : Employment Category : Full Time NORMAL_HOURS : 40 GROUP_NAME : Indirect Manager Employee Number : 1034422 PERSON_TYPE : Employee HIRE_DATE : 16/11/1983 00:00:00 TERMINATION_DATE : DATE_OF_BIRTH : 23/05/1966 00:00:00 NATIONAL_IDENTIFIER : 111 RecordDate : 43740 MAX_LAST_UPDATE_DATE : 30/10/2019 14:08:33 EMPLOYEE_NUMber : 1000522 EFFECTIVE_START_DATE : 01/10/2019 00:00:00 EFFECTIVE_END_DATE : 31/12/4712 00:00:00 CC : 0726 REGION_NAME : Head Office LOCATION_NAME : Inventory FIRST_NAME : Name MIDDLE_NAMES : Mid LAST_NAME : Last KNOWN_AS : NickName JOB_TITLE : Inventory Manager WORK_NUMber : Employment Category : Full Time NORMAL_HOURS : 40 GROUP_NAME : Indirect Manager Employee Number : 1034422 PERSON_TYPE : Employee HIRE_DATE : 16/11/1983 00:00:00 TERMINATION_DATE : DATE_OF_BIRTH : 23/05/1966 00:00:00 NATIONAL_IDENTIFIER : 111 RecordDate : 43741 MAX_LAST_UPDATE_DATE : 30/10/2019 14:08:33 ... 来做到这一点,或者与Select -expandproperty的能力相反?而不进行Group-Objectfor($i)循环的组合?

在Excel PowerQuery中的表上,这非常简单,因为您只需单击“展开并隐藏”即可。

致敬Jarek

yujian1314 回答:PowerShell-用重复行扩展数组中的数组列 对于[pscustomobject]或[hashtable]实例的输入集合:对于[System.Data.DataRow]实例的输入集合:

您可以将Select-Object -ExpandProperty与常用的-PipelineVariable参数结合起来并进行克隆(PSv3 +语法):

对于[pscustomobject][hashtable]实例的输入集合:

# Sample input array of custom objects to expand by .RecordDate
$array =
  [pscustomobject] @{ RecordDate = 1,2; OtherProp1 = 'one'; OtherProp2 = 'two' },[pscustomobject] @{ RecordDate = 3,4; OtherProp1 = 'three'; OtherProp2 = 'four' }

# Write the array elements to the pipeline,and store each in variable
# $objectOrHashtable for use in a later pipeline segment.
Write-Output $array -PipelineVariable objectOrHashtable |
  # Expand the input object's .RecordData property,i.e. send its
  # elements one by one to the next pipeline segment.
  Select-Object -ExpandProperty RecordDate | 
    ForEach-Object {
      # Clone the original input object.
      $clone = if ($objectOrHashtable -is [Management.Automation.PSCustomObject]) {
        $objectOrHashtable.psobject.Copy()
      } else { # assume [hashtable] or a type that implements [System.ICloneable]
        $objectOrHashtable.Clone()
      }
      # Assign the record date at hand to the clone...
      $clone.RecordDate = $_
      # ... and output it.
      $clone
    }

上面的结果如下:请注意,根据枚举输入对象的.RecordDate数组的元素,同时保留了所有其他属性,输出了4个对象:

RecordDate OtherProp1 OtherProp2
---------- ---------- ----------
         1 one        two
         2 one        two
         3 three      four
         4 three      four

注意:

  • 以上内容适用于两种类型的输入对象:

    • 自定义对象[pscustomobject]实例,例如由Import-Csv创建的实例)

      • 注意:出于技术原因,您不能使用-is [pscustomobject],而必须使用完整类型名称System.Management.Automation.PSCustomObject(可以省略System.前缀);由于历史原因,[pscustomobject][psobject]System.Management.Automation.PSObject)相同,并且-is [psobject]对于自定义的对象也适用对象。
    • 哈希表System.Collections.Hashtable个实例,但不是[ordered]哈希表);更一般而言,任何实现System.ICloneable的类型。

  • 对自定义对象和哈希表执行的克隆是 shallow (按成员),但是具有标量字符串和数字值就足够了。

    • 通常,ICloneable接口没有规定克隆行为的细节,因此通常不鼓励使用它。

对于[System.Data.DataRow]实例的输入集合:

克隆System.Data.DataRow实例的集合-数据表的行System.Data.DataTable-需要 custom 克隆逻辑,但是方法和结构的输出基本上是相同的:

# Create a sample DataTable...
$dt = [System.Data.DataTable]::new('sample')
# ... define the columns ...
$dt.Columns.AddRange([System.Data.DataColumn[]] (
  @{ ColumnName = 'RecordDate'; DataType = [object[]] },@{ ColumnName = 'OtherProp1'; DataType = [string] },@{ ColumnName = 'OtherProp2'; DataType = [string] }
))
# ...and add sample rows.
@{ RecordDate = 1,@{ RecordDate = 3,4; OtherProp1 = 'three'; OtherProp2 = 'four' } | % {
  $dt.Rows.Add(($dr = $dt.NewRow()))
  foreach ($entry in $_.GetEnumerator()) {
    $dr[$entry.Key] = $entry.Value 
  }  
}

# Create an auxiliary,empty clone of the input data table
# to facilitate cloning of individual rows.
$dtAux = $dt.Clone()

# Write the data rows to the pipeline,and store each in variable
# $obj for use in a later pipeline segment.
Write-Output $dt.Rows -PipelineVariable row |
  # Expand the input object's .RecordData property,i.e. send its
  # elements one by one to the next pipeline segment.
  Select-Object -ExpandProperty RecordDate |
    ForEach-Object {
      # Clone the data row at hand.
      $dtAux.Clear(); $dtAux.ImportRow($row)
      $clone = $dtAux.Rows[0]
      # Assign the record date at hand to the clone...
      $clone.RecordDate = @($_)
      # ... and output it.
      $clone
    }
本文链接:https://www.f2er.com/3136510.html

大家都在问