internal/functions/compare/Compare-ResourceList.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function Compare-ResourceList
{
    [CmdletBinding()]
    Param (
        [string[]] $ReferenceList,
        [string[]] $DifferenceList,
        [switch] $ReturnSetAction,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin
    {
        Test-GraphConnection -Cmdlet $PSCmdlet
    }
    process
    {
        if (-Not $DifferenceList) {$DifferenceList = @()}
        if (-Not $ReferenceList) {$ReferenceList = @()}

        if ($DifferenceList.count -eq 0 -and $ReferenceList.Count -eq 0) {
            return
        }        
        
        $compare = Compare-Object -ReferenceObject $ReferenceList -DifferenceObject $DifferenceList                
        if (-Not $compare) { return }

        $result = @{}
        if ($compare.SideIndicator -contains "=>" -and -not $ReturnSetAction) {
            $result["Add"] = ($compare | ? {$_.SideIndicator -eq "=>"}).InputObject
        }
        if ($compare.SideIndicator -contains "<=" -and -not $ReturnSetAction) {
            $result["Remove"] = ($compare | ? {$_.SideIndicator -eq "<="}).InputObject
        }
        if ($ReturnSetAction) {
            $result["Set"] = @($DifferenceList)
        }
        return $result
    }
}