PowerShell - How to tell if two objects are identical

后端 未结 5 1578
一个人的身影
一个人的身影 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:14

    I wrote a function that checks for exact equality:

     function Global:Test-IdenticalObjects
     {
        param(
            [Parameter(Mandatory=$true)]$Object1,
            [Parameter(Mandatory=$true)]$Object2,
            $SecondRun=$false
        )
    
        if(-not ($Object1 -is [PsCustomObject] -and $Object2 -is [PsCustomObject))
        {
            Write-Error "Objects must be PsCustomObjects"
            return
        }
    
        foreach($property1 in $Object1.PsObject.Properties)
        {
            $prop1_name = $property1.Name
            $prop1_value = $Object1.$prop1_name
            $found_property = $false
            foreach($property2 in $Object2.PsObject.Properties)
            {
                $prop2_name = $property2.Name
                $prop2_value = $Object2.$prop2_name
                if($prop1_name -eq $prop2_name)
                {
                    $found_property = $true
                    if($prop1_value -ne $prop2_value)
                    {
                        return $false
                    }
                }
            } # j loop
            if(-not $found_property) { return $false }
        } # i loop
        if($SecondRun)
        {
            return $true
        } else {
            Test-IdenticalObjects -Object1 $Object2 -Object2 $Object1 -SecondRun $true
        }
     } # function
    

提交回复
热议问题