-1

What I'm trying to do is compare the data from two pscustomobject arrays, Then have the data that doesnt matches saved into an array. That array will then be exported to a CSV file. This is what i have.

function DataCompare {

$NotMatch = @()
#Compares the two arrays together and saves the ones that are different in the InfoFusion into ResultsFromFusion
$ResultsFromFusion = Compare-Object -ReferenceObject $Global:InfoFusion -DifferenceObject $Global:InfoPPMS -Property programmeLevel1, programmeLevel2, programmeLevel3, programmeLevel4 -PassThru | Where-Object SideIndicator -eq '<='

#Loops through the fusion results that dont match
ForEach ($Project in $ResultsFromFusion){
    
    #Gets the matching data from the PPMS data so the NotMatch csv has both the fusion and ppms data
    $PPMSFromDiff = $Global:InfoPPMS -match $Project.ProjectNumber
    $NotMatch += @([pscustomobject]@{ProjectId = $($Project.ProjectId);
                                     ProjectNumber = $($Project.ProjectNumber);
                                     programmeLevelFusion1 = $($Project.programmeLevel1); 
                                     programmeLevelPPMS1 = $($PPMSFromDiff.programmeLevel1); 
                                     programmeLevelFusion2 = $($Project.programmeLevel2); 
                                     programmeLevelPPMS2 = $($PPMSFromDiff.programmeLevel2);
                                     programmeLevelFusion3 = $($Project.programmeLevel3); 
                                     programmeLevelPPMS3 = $($PPMSFromDiff.programmeLevel3); 
                                     programmeLevelFusion4 = $($Project.programmeLevel4); 
                                     programmeLevelPPMS4 = $($PPMSFromDiff.programmeLevel4)})
    }#Loop End
$NotMatch | export-csv -Path $NotMatchPath -NoTypeInformation -Encoding utf8
}#Function End

However, when doing this the CSV saves with System.Object[] in some of the cells and its switching out charaters like "-" with "?" even though i added encoding utf8 for it not to do this. Can anyone help with this.

Edit: This is what the two arrays that ar ebeing compared look like.

$Global:InfoPPMS += @([pscustomobject]@{ProjectId = $($Project.ProjectId);
                                                ProjectNumber = $($Project.ProjectNumber);
                                                programmeLevel1 = $($programmeLevel1); 
                                                programmeLevel2 = $($programmeLevel2); 
                                                programmeLevel3 = $($programmeLevel3); 
                                                programmeLevel4 = $($programmeLevel4)})
$Global:InfoFusion += @([pscustomobject]@{ProjectId = $($Project.ProjectId); 
                                                          ProjectNumber = $($Project.ProjectNumber);
                                                          programmeLevel1 = $($RestMethID.programmeLevel1); 
                                                          programmeLevel2 = $($RestMethID.programmeLevel2); 
                                                          programmeLevel3 = $($RestMethID.programmeLevel3); 
                                                          programmeLevel4 = $($RestMethID.programmeLevel4)})

this is how they look.

Selene
  • 31
  • 6
  • Compare-Object doesn't save to csv. You can see in your code that you are manually creating your object. You need to build your object to be able to properly convert to csv. I'd open a different question about the encoding issue, providing a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) so that we can have a chance at helping you. – Doug Maurer Apr 11 '23 at 13:40
  • @DougMaurer Where have i gone wrong with my object then if i haven't built it correctly for it to export to csv. – Selene Apr 11 '23 at 13:52
  • csv doesn't support arrays like json does. You'll have to do something extra like -join ' ' first. Something like an em dash should save properly in utf8. – js2010 Apr 11 '23 at 13:58
  • You might try this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `$InfoFusion |LeftJoin $InfoPPMS -on programmeLevel1, programmeLevel2, programmeLevel3, programmeLevel4 -name Fusion, PPMS` Or as it concerns all properties: `$InfoFusion |LeftJoin $InfoPPMS -on * -name Fusion, PPMS` – iRon Apr 11 '23 at 14:01
  • As an aside, try to [avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) and the `export-csv` [cmdlet should go outside the loop](https://learn.microsoft.com/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations#avoid-wrapping-cmdlet-pipelines). – iRon Apr 11 '23 at 14:02
  • @iRon the export-csv is outside the loop. If i should use += then what should i use – Selene Apr 12 '23 at 11:17
  • @Selene, see the related links in the comment for what to do instead. – iRon Apr 12 '23 at 12:14
  • @iRon Ok thanks. I've had a look and i've made my foreach now: $Global:InfoFusion = ForEach. And removed $Global:InfoFusion +=. However in my code i have and incrementing number, so i now how far the script is along. $I and $I++ inside the same foreach as the array data, but i dont want this included. Is there a way to leave this out. and still have it display every loop through – Selene Apr 13 '23 at 12:33

1 Answers1

0

Joining an array with commas that can be exported into a csv:

[pscustomobject]@{array=1..10} | % { $_.array = $_.array -join ','; $_ }

array
-----
1,2,3,4,5,6,7,8,9,10
js2010
  • 23,033
  • 6
  • 64
  • 66