private/resolveEntityReferences.ps1


function resolveEntityReferences {
    Param(
        
        [String]$RightsPrefix,
        [Hashtable]$RightsAndPrincipals
    )
    begin{
        $Netbios = (get-addomain).netBiosName
    }
    Process {
        foreach ($item in $RightsAndPrincipals.GetEnumerator()) {
            foreach ($entity in $item.value) {
                if ($null -ne $entity) {
                    $FilterValue = $entity
                    switch ($item.key) {
                        "SIDS" { $FilterType = "ObjectSID" }
                        "Principals"  { $FilterType = "Name" }
                        "Rights" {                        
                            $FilterType = "Name"
                            $FilterValue = "{0}-{1}" -f $RightsPrefix, $entity
                        }
                    }
                    $Filter = "{0} -eq '{1}'" -f $FilterType, $FilterValue
                    $foundObjects = get-adObject -filter $Filter -properties ObjectSID | select-object Name, ObjectSID, ObjectClass
                    if (-not $foundObjects) {
                        $foundObjects = [PSCustomObject]@{
                            Name = $null
                            ObjectSID = $Null
                            ObjectClass = "Group"
                            NetBIOS = $null
                        }
                        $FoundObjects."$FilterType" = $FilterValue
                    }
                    $FoundObjects | foreach-object {
                        [PSCustomObject]@{
                            Type=$item.key
                            Name = $_.name
                            SID = $_.ObjectSID
                            ObjectClass = $_.objectClass
                            GPOSIDRef = if ($_.objectSID) { "*$($_.ObjectSID)" } else {$Null}
                            NetBIOS = "{0}\{1}" -f $netBios, $_.name
                        }
                    }
                }
            }
        }
    }
}