question

Shell-4399 avatar image
0 Votes"
Shell-4399 asked RichMatheisen-8856 commented

How to Join PsCustomObjects OR Append Elements to the TOP of an Existing PsCustomObject For Export to CSV

How do we join PSCustomObjects or append new items to be start (not end) of an existing PSCustomObject please?

Basic example

 $matchFound    = $true
 $invalidAddress = $true

 # This object will have approx 4 fields in it - reasons why a record is invalid
 $myObj1 = [PSCustomObject]@{
         MatchFound      = "True"
         InvalidAddress  = "True"
     }


 # This object will have approx 50 fields in it
 $myObj2 = [PSCustomObject]@{
         FirstName       = "Bill"
         Surname         = "Jones"
     }
    
 # VALID RECORDS:
 #$myObj2 | export-csv "C:\temp6\valid.csv" -NoTypeInformation -Append

 # INVALID RECORDS:
 #$myObj1,$myObj2 | export-csv "C:\temp6\invalid.csv" -NoTypeInformation -Append
 # OR 
 #$matchFound,$invalidAddress,$myObj2 | export-csv "C:\temp6\invalid.csv" -NoTypeInformation -Append
    
 <#
 MatchFound  InvalidAddress  Firstname  Surname
 ----        -----           ----       -----
 True        True           Bill       Jones
 #>


eg:
If $myObj1 contains MatchFound = True and InvalidAddress = False, then I only want to append $myObj2 to a csv file (eg: valid.csv)
else I want to append a combination of both objects ($myObj1 and $myObj2) to a single line in a csv file (eg: invalid.csv)

Thanks

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

Here's one way to do it -- Don't use PSCustomObjects until you need them. Use an ordered hash and use them to create the PSCustomObjects when you need them.

 # invalid hash
 $o1 = [ordered]@{
     MatchFound      = "True"
     InvalidAddress  = "True"
 }
 #valid hash
 $o2 = [ordered]@{
     FirstName       = "Bill"
     Surname         = "Jones"
     C3 = "cp"
     XX50 = "xxp"
 }
 [PSCustomObject]$o2 | Export-Csv c:\junk\valid.csv -NoTypeInformation
    
    
 $o3 = [ordered]@{}
 $o1, $o2 |
     ForEach-Object{
         $_.GetEnumerator() |
             ForEach-Object{
                 $o3[$_.Key] = $_.Value
             }
     }
 [PSCustomObject]$o3 | Export-Csv c:\junk\invalid.csv -NoTypeInformation
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, I'll give it a try.

0 Votes 0 ·

I went googling as I hadn't used GetEnumerator before & found that you can just use this for $o3 instead, thanks :)

 $o3 = [PSCustomObject]($o1+$o2)
 $o3 | Export-Csv c:\junk\invalid.csv -NoTypeInformation
0 Votes 0 ·

Or just eliminate $o3 completely!

 [PSCustomObject]($o1+$o2) | Export-Csv c:\junk\invalid.csv -NoTypeInformation
1 Vote 1 ·