PowerShell Export-Csv在输出中重复最后一个值-解决方案

时间:2018-08-17 16:14:26

标签: powershell export-csv

场景: 在脚本中,我创建一个对象,添加公共信息,然后循环更新每个对象的子信息,然后将该对象添加到对象数组中。 我的详细说明显示了添加到数组中的对象中的值正确(报告数组中的最后一项),但是Export-Csv重复了最后一个对象值。 如果每次创建一个新对象,我都可以解决。重用同一对象会重复最后一个值,即使我在数组中的对象上选择*时也可以看到正确的值。关于数组对象必须有一些东西,例如为同一对象复制的GUID。 在Windows 7,Windows 2008R2,Windows 2010R2上验证了PowerShell 5.1

解决方案: 将对象添加到对象数组时,请勿重复使用。

输出:

#TYPE MyGpoSetting
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName","SubState","SubValue","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"

脚本:

Add-Type -TypeDefinition @"
   public struct MyGpoSetting
   {
      public string  SetName;
      public string  SetCategory;
      public string  SetType;
      public string  SetState;
      public string  SetValue;
      public string  SetData;
      public string  SetNote;
      public string  SubName;
      public string  SubState;
      public string  SubValue;
      public string  GpoDomain;
      public string  GpoName;
      public string  GpoLinks;
      public string  GpoGuid;
   }
"@
$aoMyGpoSetting = @();
$oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName       = 'SetName';
$oMyGpoSetting.SetCategory   = 'SetCategory';
$oMyGpoSetting.SetType       = 'SetType';
$oMyGpoSetting.SetState      = 'SetState';
$oMyGpoSetting.SetValue      = 'SetValue';
$oMyGpoSetting.SetData       = 'SetData';
$oMyGpoSetting.SetNote       = 'SetNote';
$oMyGpoSetting.SubName       = 'SubName1';
$oMyGpoSetting.SubState      = 'SubState1';
$oMyGpoSetting.SubValue      = 'SubValue1';
$oMyGpoSetting.GpoDomain     = 'GpoDomain';
$oMyGpoSetting.GpoName       = 'GpoName';
$oMyGpoSetting.GpoLinks      = 'GpoLinks';
$oMyGpoSetting.GpoGuid       = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
#--- $oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName       = 'SetName';
$oMyGpoSetting.SetCategory   = 'SetCategory';
$oMyGpoSetting.SetType       = 'SetType';
$oMyGpoSetting.SetState      = 'SetState';
$oMyGpoSetting.SetValue      = 'SetValue';
$oMyGpoSetting.SetData       = 'SetData';
$oMyGpoSetting.SetNote       = 'SetNote';
$oMyGpoSetting.SubName       = 'SubName2';
$oMyGpoSetting.SubState      = 'SubState2';
$oMyGpoSetting.SubValue      = 'SubValue2';
$oMyGpoSetting.GpoDomain     = 'GpoDomain';
$oMyGpoSetting.GpoName       = 'GpoName';
$oMyGpoSetting.GpoLinks      = 'GpoLinks';
$oMyGpoSetting.GpoGuid       = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
#--- $oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName       = 'SetName';
$oMyGpoSetting.SetCategory   = 'SetCategory';
$oMyGpoSetting.SetType       = 'SetType';
$oMyGpoSetting.SetState      = 'SetState';
$oMyGpoSetting.SetValue      = 'SetValue';
$oMyGpoSetting.SetData       = 'SetData';
$oMyGpoSetting.SetNote       = 'SetNote';
$oMyGpoSetting.SubName       = 'SubName3';
$oMyGpoSetting.SubState      = 'SubState3';
$oMyGpoSetting.SubValue      = 'SubValue3';
$oMyGpoSetting.GpoDomain     = 'GpoDomain';
$oMyGpoSetting.GpoName       = 'GpoName';
$oMyGpoSetting.GpoLinks      = 'GpoLinks';
$oMyGpoSetting.GpoGuid       = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
$aoMyGpoSetting | Export-Csv -Path 'c:\temp\export.csv' -Encoding 'ASCII';

1 个答案:

答案 0 :(得分:1)

这按预期工作。将对象添加到数组(+=)不会复制该对象,而是在下一个“插槽”中为其添加引用/指针。因此,实际上您是向同一个对象添加三个引用。就像您的电话簿中有3个条目是您最好的朋友一样:

John Smith - 01234 5678
Jonnie - 01234 5678
Smith, John - 01234 5678

无论您打给谁,都会带您到同一个人。

类似地,PowerShell每次显示数组中的对象时,实际上实际上是返回到相同的源对象并向您显示。这就是为什么它们都具有与您添加的最后一个属性相同的原因-实际上它们都是相同的。

您已经发现,每次创建一个新对象都是继续的方法。