Get-MsalAccount.ps1

<#
.SYNOPSIS
    Get user from token cache of application.
.DESCRIPTION
 
.EXAMPLE
    PS C:\>$ClientApplication = Get-MsalClientApplication -ClientId '00000000-0000-0000-0000-000000000000'
    PS C:\>$ClientApplication | Get-MsalAccount
    Get all accounts from client application cache.
#>

function Get-MsalAccount {
    param
    (
        # Client application
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ClientApplication', Position = 0)]
        [Microsoft.Identity.Client.IClientApplicationBase] $ClientApplication,
        # Information of a single account.
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Accounts', Position = 0)]
        [Microsoft.Identity.Client.IAccount[]] $Accounts,
        # The username in UserPrincipalName (UPN) format.
        [Parameter(Mandatory = $false)]
        [string] $Username
    )

    if ($PSCmdlet.ParameterSetName -eq 'ClientApplication') {
        [Microsoft.Identity.Client.IAccount[]] $Accounts = $ClientApplication.GetAccountsAsync().GetAwaiter().GetResult()
    }

    if ($Username) {
        return $Accounts | Where-Object Username -eq $Username
    }
    else {
        return $Accounts
    }
}

# SIG # Begin signature block
# MIIckwYJKoZIhvcNAQcCoIIchDCCHIACAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBe92h1T3h2eTvd
# +l+vqyFJg+5I9n0phwspY0bUr5NNiqCCF50wggUmMIIEDqADAgECAhAKbwamSf02
# TrzqY8wkoMRzMA0GCSqGSIb3DQEBCwUAMHIxCzAJBgNVBAYTAlVTMRUwEwYDVQQK
# EwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xMTAvBgNV
# BAMTKERpZ2lDZXJ0IFNIQTIgQXNzdXJlZCBJRCBDb2RlIFNpZ25pbmcgQ0EwHhcN
# MjAwMzMxMDAwMDAwWh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