Public/Set-MsrcAdalAccessToken.ps1

Function Set-MSRCAdalAccessToken {
[CmdletBinding(SupportsShouldProcess)]
Param(
    [Parameter()]
    [Alias('ClientId')]
    [string]$ID
)
Begin {}
Process {
    if ([AppDomain]::CurrentDomain.SetupInformation.TargetFrameworkName -like "*v5.*") {
        throw ".Net Core v5.x is not currently supported"
    }

    if ($PSCmdlet.ShouldProcess('Set the MSRCApiKey using MSRCAdalAccessToken')) {
        Add-Type -Path "$PSScriptRoot/../Microsoft.IdentityModel.Clients.ActiveDirectory.dll" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

        $authority = 'https://login.windows.net/microsoft.onmicrosoft.com/'

        $authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($authority)

        $rUri = New-Object System.Uri -ArgumentList 'http://localhost:50000'

        $promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto

        $ResourceId = $ID

        $ClientId = $ID

        $global:MSRCAdalAccessToken = $null

        if ($null -ne $authContext.AcquireToken) {
            $global:MSRCAdalAccessToken = $authContext.AcquireToken($ResourceId, $ClientId, $rUri,$promptBehavior)
        } elseif ($null -ne $authContext.AcquireTokenAsync) {
            $platformParams = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters($promptBehavior)
            $task = $authContext.AcquireTokenAsync($ResourceId, $ClientId, $rUri,$platformParams)
            $task.Wait()
            $global:MSRCAdalAccessToken = $task.Result
        }

        if ($null -ne $global:MSRCAdalAccessToken) {
            Write-Verbose -Message $global:MSRCAdalAccessToken.AccessToken
            Write-Verbose -Message "Successfully set your Access Token required by cmdlets of this module. Calls to the MSRC APIs will now use your access token."
        } else {
            throw "Failed Acquiring Access Token!"
        }
    }
}
End {}
}