Remove-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
<#
.SYNOPSIS
    Remove client application from local session cache.
.DESCRIPTION
    This cmdlet will remove a client application object from the local session cache.
.EXAMPLE
    PS C:\>Remove-MsalClientApplication $ClientApplication
    Remove specified client application from local session cache.
#>

function Remove-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
    )

    switch ($PSCmdlet.ParameterSetName) {
        "PublicClient" {
            $ClientApplication = $PublicClientApplication
            $Result = $PublicClientApplications.Remove($ClientApplication)
            break
        }
        "ConfidentialClient" {
            $ClientApplication = $ConfidentialClientApplication
            $Result = $ConfidentialClientApplications.Remove($ClientApplication)
            break
        }
    }

    if (!$Result) {
        $Exception = New-Object ArgumentException -ArgumentList 'The client application provided was not found in session cache.'
        Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ObjectNotFound) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'RemoveMsalClientApplicationFailureNotFound' -TargetObject $ClientApplication -ErrorAction Stop
    }
}