MrMr
MrMr

Reputation: 493

Compare Two Objects Properties and Obtain Missing Items

I'm attempting to compare two object properties called "name" and that contain the same type of data, in this case server host names. The objects are not identical AKA do not have the same properties. In short, I'm attempting to compare two lists of server names and determine where (which object) they are missing from.

I'm looking to find the items (server host names) that are missing from each object. When something is found I'm hoping to obtain all related properties for the item in the given object that it was found in. I can do the compare-object successfully, but don't know how to get the results I'm looking for.

I'm thinking two new objects could be created for each, that list the items that were not found in the other object maybe? Or do I somehow reference the previous objects with the output from compare-object and produce some formatted output?

This code currently produces a blank file.

Data Format:

$SNObject:

name,ip,class

server-place.com,10.10.10.10,windows server

$QRObject:

name,date,ip,general,device type

server-place1.com,11.11.11.11,random info,linux server

Code Example:

$compare = compare-object $SNObject $QRObject -property Name    | 

foreach  { 
  if ($_.sideindicator -eq '<=')
    {$_.sideindicator = $PathToQRReport }

  if ($_.sideindicator -eq '=>')
    {$_.sideindicator = $PathToSNReport}
 }

$compare | 
select @{l='Value';e={$_.InputObject}},@{l='File';e={$_.SideIndicator}} |
Out-File -FilePath C:\Temp\MissingOutputs1.txt

Upvotes: 0

Views: 563

Answers (1)

thepip3r
thepip3r

Reputation: 2935

Ahh... just thought of an alternative that may give you exactly what you're looking for but in a slightly different way:

   ## Join both arrays into single array and then group it on the property name that has a shared value, 'name'
$all = @()
$group = @($SNObject + $QRObject)
$group | Group-Object -Property name | % {
    ## Create a custom object that contains all possible properties plus a directionality indicator ('source')
    $n = New-Object PSObject -Property @{
        'name' = ''
        'date' = ''
        'ip' = ''
        'general' = ''
        'platform' = ''
        'source' = ''
    }

    if ($_.Count -eq 1) {
        ## Loop through the grouped results and determine their source and write properties based off of their source
        foreach ($i in $_.Group) {

            if (@($i | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name) -contains 'date' ) {
                ## This value came from $QRObject which apparently is the only dataset that contains a date property
                $n.source = 'QRObject'
                $n.date = $i.date
                $n.general = $i.general
                $n.platform = $i.'device type'
            } else {
                ## This object does not contain the 'date' property, therefore it came from $SNObject    
                $n.source = 'SNObject'
                $n.platform = $i.class
            }

            ## write out common properties
            $n.name = $i.name
            $n.ip = $i.ip

            ## add the custom PSObject back to a master array with all formatted properties
            $all += $n
        }
    }
}

$all | out-whereever-you-want

Upvotes: 1

Related Questions