internal/functions/resolve/Resolve-AccessPackageAssignmentPolicy.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
function Resolve-AccessPackageAssignmentPolicy
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string] $InputReference,
        [switch] $DontFailIfNotExisting,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin {
        $AccessPackageAssignmentPolicyReference = Resolve-String -Text $AccessPackageAssignmentPolicyReference
    }
    process
    {            
        try {
            if ($AccessPackageAssignmentPolicyReference -match $script:guidRegex) {
                $accessPackageAssignmentPolicy = Get-MgEntitlementManagementAccessPackageAssignmentPolicy -AccessPackageAssignmentPolicyId $AccessPackageAssignmentPolicyReference
            }
            else {
                $accessPackageAssignmentPolicy = Get-MgEntitlementManagementAccessPackageAssignmentPolicy -Filter "displayName eq '$AccessPackageAssignmentPolicyReference'"
            }
            
            if (-Not $accessPackageAssignmentPolicy -and -Not $DontFailIfNotExisting) { throw "Cannot find user $AccessPackageAssignmentPolicyReference" } 
            elseif (-Not $accessPackageAssignmentPolicy -and $DontFailIfNotExisting) { return }

            if ($accessPackageAssignmentPolicy.count -gt 1) { throw "Got multiple Access Package Catalogs for $AccessPackageAssignmentPolicyReference" }
            return $accessPackageAssignmentPolicy
        }
        catch {
            Write-PSFMessage -Level Warning -String 'TMF.CannotResolveResource' -StringValues "AccessPackageAssignmentPolicy" -Tag 'failed' -ErrorRecord $_
            $Cmdlet.ThrowTerminatingError($_)                
        }            
    }
}