internal/functions/resolve/Resolve-DirectoryRoleTemplate.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
42
function Resolve-DirectoryRoleTemplate
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string] $InputReference,
        [switch] $DontFailIfNotExisting,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin {
        $InputReference = Resolve-String -Text $InputReference
    }
    process
    {            
        try {
            if (-Not $script:cache["allRoleTemplates"]) {
                $script:cache["allRoleTemplates"] = (Invoke-MgGraphRequest -Method GET -Uri "$script:graphBaseUrl/directoryRoleTemplates").Value `
                                            | Select-Object @{n = "id"; e = {$_["id"]}}, @{n = "displayName"; e = {$_["displayName"]}}
            }
            
            if ($InputReference -match $script:guidRegex) {
                $roleTemplate = $script:cache["allRoleTemplates"] | Where-Object {$_.id -eq $InputReference}
            }
            else {
                $roleTemplate = $script:cache["allRoleTemplates"] | Where-Object {$_.displayName -eq $InputReference}
            }

            if (-Not $roleTemplate -and -Not $DontFailIfNotExisting) { throw "Cannot find directoryRoleTemplate $InputReference." } 
            elseif (-Not $roleTemplate -and $DontFailIfNotExisting) { return }

            if ($roleTemplate.count -gt 1) { throw "Got multiple directoryRoleTemplates for $InputReference" }
            return $roleTemplate.Id
        }
        catch {
            Write-PSFMessage -Level Warning -String 'TMF.CannotResolveResource' -StringValues "DirectoryRoleTemplate" -Tag 'failed' -ErrorRecord $_
            $Cmdlet.ThrowTerminatingError($_)                
        }            
    }
}