functions/entitlementManagement/accessPackageResource/Register-TmfAccessPackageResource.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function Register-TmfAccessPackageResource
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string] $displayName,
        [Parameter(Mandatory = $true)]
        [string] $resourceIdentifier,
        [string] $description = "Access Package Resource has been created with Tenant Management Framework",

        [ValidateSet("SharePointOnline", "AadApplication", "AadGroup")]
        [string] $originSystem = "AadGroup",
        [string] $catalog = "General",
        [ValidateSet("AadGroup", "Application", "Sharepoint Online Site")]
        [string] $resourceType = "AadGroup",
        [ValidateSet("Member", "Owner")]
        [string] $resourceRole = "Member",

        [bool] $present = $true,
        [string] $sourceConfig = "<Custom>",

        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet,
        [switch] $PassThru
    )
    
    begin
    {
        $resourceName = "accessPackageResources"
        if (!$script:desiredConfiguration[$resourceName]) {
            $script:desiredConfiguration[$resourceName] = @()
        }

        if ($script:desiredConfiguration[$resourceName].displayName -contains $displayName) {            
            $alreadyLoaded = $script:desiredConfiguration[$resourceName] | Where-Object {$_.displayName -eq $displayName}
        }

    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }                

        $object = [PSCustomObject]@{        
            displayName = $displayName
            description = $description
            resourceIdentifier = $resourceIdentifier
            resourceType = $resourceType
            resourceRole = $resourceRole
            originSystem = $originSystem
            catalog = $catalog
            present = $present
        }        
        
        <# NOT REQUIRED ATM
        @() | foreach {
            if ($PSBoundParameters.ContainsKey($_)) {
                Add-Member -InputObject $object -MemberType NoteProperty -Name $_ -Value $PSBoundParameters[$_]
            }
        } #>

    
        Add-Member -InputObject $object -MemberType ScriptMethod -Name "catalogId" -Value {
            Resolve-AccessPackageCatalog -InputReference $this.catalog
        }
        Add-Member -InputObject $object -MemberType ScriptMethod -Name "originId" -Value {
            switch ($this.resourceType) { # Resolve originId (eg. get the ObjectId of a group resource)
                "AadGroup" {
                     $originId = Resolve-Group -InputReference $this.resourceIdentifier -DontFailIfNotExisting
                }
                default {
                    $originId = $this.resourceIdentifier
                }
            }
            $originId
        }
        Add-Member -InputObject $object -MemberType ScriptMethod -Name "roleOriginId" -Value { $originId = $this.originId(); if ($originId) { "{0}_{1}" -f $this.resourceRole, $originId }}
        Add-Member -InputObject $object -MemberType ScriptMethod -Name Properties -Value { ($this | Get-Member -MemberType NoteProperty).Name }

        if ($alreadyLoaded) {
            $script:desiredConfiguration[$resourceName][$script:desiredConfiguration[$resourceName].IndexOf($alreadyLoaded)] = $object
        }
        else {
            $script:desiredConfiguration[$resourceName] += $object
        }
    }
    end 
    {
        if ($PassThru) {
            $object
        }        
    }
}