PowerShell - How to tell if two objects are identical

后端 未结 5 1556
一个人的身影
一个人的身影 2021-01-24 17:05

Let\'s say you have two objects that are identical (meaning they have the same properties and the same values respectively).

How do you test for equality?

5条回答
  •  悲哀的现实
    2021-01-24 17:19

    If you'd like to test for equality for every object property, one at a time, in order to compare and contrast two objects and see which individual pieces are different, you can use the following function, adapted from this article on how to compare all properties of two objects in Windows PowerShell

    Function Compare-ObjectProperties {
        Param(
            [PSObject]$leftObj,
            [PSObject]$rightObj 
        )
    
        $leftProps = $leftObj.PSObject.Properties.Name
        $rightProps = $rightObj.PSObject.Properties.Name
        $allProps = $leftProps + $rightProps | Sort | Select -Unique
    
        $props = @()
    
        foreach ($propName in $allProps) {
    
            # test if has prop
            $leftHasProp = $propName -in $leftProps
            $rightHasProp = $propName -in $rightProps
    
            # get value from object
            $leftVal = $leftObj.$propName
            $rightVal = $rightObj.$propName
    
            # create custom output - 
            $prop = [pscustomobject] @{   
                Match = $(If ($propName -eq "SamAccountName" ) {"1st"} Else {
                            $(If ($leftHasProp -and !$rightHasProp ) {"Left"} Else {
                                $(If ($rightHasProp -and !$leftHasProp ) {"Right"} Else {
                                    $(If ($leftVal -eq $rightVal ) {"Same"} Else {"Diff"})
                                })
                              })
                         })
                PropName = $propName
                LeftVal = $leftVal
                RightVal = $rightVal
            }
    
            $props += $prop
        }
    
        # sort & format table widths
        $props | Sort-Object Match, PropName | Format-Table -Property `
                   @{ Expression={$_.Match}; Label="Match"; Width=6}, 
                   @{ Expression={$_.PropName}; Label="Property Name"; Width=25}, 
                   @{ Expression={$_.LeftVal }; Label="Left Value";    Width=40}, 
                   @{ Expression={$_.RightVal}; Label="Right Value";   Width=40}
    
    }
    

    And then use like this:

    $adUser1 = Get-ADUser 'Grace.Hopper' -Properties *
    $adUser2 = Get-ADUser 'Katherine.Johnson' -Properties *   
    Compare-ObjectProperties $adUser1 $adUser2
    

    Couple Interesting Notes:

    • How to Test if Element Has Property
    • How to Get Property Value by Name
    • How to Create a Custom PS Object
    • How to Create a Nested Conditional / Ternary Operator
    • How to Format Table with fixed Widths
    • Attempted to Colorize output with VT Escape Sequences or Write-PSObject, but couldn't get it to work with fixed column widths which took priority

提交回复
热议问题