Functions/Compare-MSPCompleteGroup.ps1

<#
.SYNOPSIS
    This function performs a comparison between a reference group object and a comparison group object.
.DESCRIPTION
    This function performs a comparison between a reference group object and a comparison group object.
    It returns true if the objects match in both property values and extended property values,
    and false otherwise.
#>

function Compare-MSPCompleteGroup {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param (
        # The group used as the reference object.
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSObject]$referenceGroup,

        # The group used as the comparison object.
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSObject]$comparisonGroup
    )

    # Get list of properties to compare
    $propertiesToCompare = Get-MSPCompleteGroupPropertyList

    # Compare properties
    foreach ($property in $propertiesToCompare) {
        if ($referenceGroup.$property -ne $comparisonGroup.$property) {
            Write-Error "Property '$($property)' does not match for group '$($referenceGroup.Name)' - Reference: '$($referenceGroup.$property)' Comparison: '$($comparisonGroup.$property)'"
            return $false
        }
    }

    # Get list of extended properties to compare
    $extendedPropertiesToCompare = Get-MSPCompleteGroupExtendedPropertyList

    # Compare extended properties
    foreach ($property in $extendedPropertiesToCompare) {
        if ($referenceGroup.ExtendedProperties.$property -ne $comparisonGroup.ExtendedProperties.$property) {
            Write-Error "Extended property '$($property)' does not match for group '$($referenceGroup.Name)' - Reference: '$($referenceGroup.ExtendedProperties.$property)' Comparison: '$($comparisonGroup.ExtendedProperties.$property)'"
            return $false
        }
    }

    # Compare members
    $referenceGroupMembers = ConvertTo-Array ($referenceGroup.Members | Sort-Object)
    $comparisonGroupMembers = ConvertTo-Array ($comparisonGroup.Members | Sort-Object)
    $comparisonResult = Compare-Object -ReferenceObject $referenceGroupMembers -DifferenceObject $comparisonGroupMembers
    if ($null -ne $comparisonResult) {
        Write-Error "Member lists for group '$($referenceGroup.Name)' do not match - `r`nReference: $($referenceGroupMembers -join ',')`r`nComparison: $($comparisonGroupMembers -join ',')"
        return $false
    }

    # The two objects are the same
    return $true
}