Powershell脚本将多值单元格扩展为行

时间:2018-03-08 08:39:08

标签: powershell

我有一个包含多值数据的单元格的csv文件。我正在使用powershell尝试将多个值拆分为在列中运行的行,不幸的是,我可以让它为两列工作,但第三列不显示。我在下面输入了输入和所需输出的示例。

   $x = Import-Csv "D:\EmailData.csv"
   $($x | Foreach {
   $current = $_
   $current.members -split ';' | foreach  {
    [PSCUstomObject]@{
        Name  = $current.DLGroup
        Owners  = $Current.Owners
        Members  = $_
        }
    }
   }  | Foreach {
   $current = $_
   $current.owners -split ';' | foreach  {
    [PSCUstomObject]@{
        Name  = $current.DLGroup
        Owners  = $_
        Members  = $Current.members
        }
    }
    } )
 This is the input file as text(comma delimited and ; between cell values)

  Name,Owners,Members  
  DLGroup1,bob@test.com;jen@test.com,phi@test.com;sue@test.com
  DLgroup2,mark@test.com;john@test.com,pete@test.com;john@test.com
  DLGroup3,pete@test.com;sue@test.com,mark@test.com;jen@test.com

这就是我希望处理后数据的方式:

Name,Owners,Members  
DLGroup1,bob@test.com,sue@test.com  
DLGroup1,jen@test.com,phi@test.com  
DLgroup2,mark@test.com,john@test.com  
DLgroup2,john@test.com,pete@test.com  
DLGroup3,pete@test.com,jen@test.com  
DLGroup3,sue@test.com,mark@test.com  

1 个答案:

答案 0 :(得分:0)

Import-Csv 'Data.csv'|ForEach-Object{
  foreach ($value in ($_.Members -split ';')) {
    foreach  ($Line in ($_.Owners -split ';')){$_|
Select-Object -Exclude Members,Owners -Property *,@{n='Members';e={$value}},@{n='Owners';e={$Line}}
}
}}|Export-Csv 'file.csv'