user7864623
user7864623

Reputation: 1

PowerShell Export-Csv repeating last value in output - solution

SCENARIO: In my script, I create an object, add the common info, and then updated the sub info for each object in a loop and add that object to an array of objects. My verbose shows that the values are correct in the object added to the array (reporting the last item in the array), but Export-Csv repeats the last object values. I am able to resolve this if I create a new object each time. Reusing the same object repeats the last value, even though I can see it correct when I select * on the objects in the array. There must be something about the array object, such as a guid that is duplicated for the same object. PowerShell 5.1 verified on Windows 7, Windows 2008R2, Windows 2010R2

SOLUTION: Do not reuse an object when adding it to an array of objects.

OUTPUT:

#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"

SCRIPT:

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';

Upvotes: 0

Views: 167

Answers (1)

boxdog
boxdog

Reputation: 8432

This is working as expected. Adding the object to the array (+=) doesn't copy the object, but instead adds a reference/pointer to it in the next 'slot'. So in effect you are adding three references to the same object. It's like having 3 entries in your phonebook for your best friend:

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

Whichever one you call, gets you through to the exact same person.

Similarly, each time PowerShell displays an object from your array, it is actually going back to the same source object and showing it to you. That is why all of them have the same properties as the last one you added - they are in fact all that same one.

As you've discovered, creating a new object each time is the way to proceed.

Upvotes: 1

Related Questions