Add-MsalClientApplication.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
<#
.SYNOPSIS
    Add client application to local session cache.
.DESCRIPTION
    This cmdlet will add a client application object to the local session cache.
.EXAMPLE
    PS C:\>Add-MsalClientApplication $ClientApplication
    Add client application to the local session cache.
.EXAMPLE
    PS C:\>Add-MsalClientApplication $ClientApplication -PassThru
    Add client application to the local session cache and return application object.
#>

function Add-MsalClientApplication {
    [CmdletBinding(DefaultParameterSetName='PublicClient')]
    [OutputType([Microsoft.Identity.Client.PublicClientApplication],[Microsoft.Identity.Client.ConfidentialClientApplication])]
    param
    (
        # Public client application
        [Parameter(Mandatory=$true, ParameterSetName='PublicClient', Position=0, ValueFromPipeline=$true)]
        [Microsoft.Identity.Client.IPublicClientApplication] $PublicClientApplication,
        # Confidential client application
        [Parameter(Mandatory=$true, ParameterSetName='ConfidentialClient', Position=0, ValueFromPipeline=$true)]
        [Microsoft.Identity.Client.IConfidentialClientApplication] $ConfidentialClientApplication,
        # Returns client application
        [Parameter(Mandatory=$false)]
        [switch] $PassThru
    )

    switch ($PSCmdlet.ParameterSetName) {
        "PublicClient" {
            $ClientApplication = $PublicClientApplication
            if (!$PublicClientApplications.Contains($ClientApplication)) {
                $PublicClientApplications.Add($ClientApplication)
            }
            else {
                $Exception = New-Object ArgumentException -ArgumentList 'The client application provided already exists in the session cache.'
                Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ResourceExists) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'AddMsalClientApplicationFailureAlreadyExists' -TargetObject $ClientApplication #-ErrorAction Stop
            }
            break
        }
        "ConfidentialClient" {
            $ClientApplication = $ConfidentialClientApplication
            if (!$ConfidentialClientApplications.Contains($ClientApplication)) {
                $ConfidentialClientApplications.Add($ClientApplication)
            }
            else {
                $Exception = New-Object ArgumentException -ArgumentList 'The client application provided already exists in the session cache.'
                Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ResourceExists) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'AddMsalClientApplicationFailureAlreadyExists' -TargetObject $ClientApplication #-ErrorAction Stop
            }
            break
        }
    }

    if ($PassThru) {
        Write-Output $ClientApplication
    }
}