Created
February 5, 2022 23:43
-
-
Save M1kep/c202326efb118558eedc07171e897faa to your computer and use it in GitHub Desktop.
Retrieves the token needed to make calls to the main.iam.ad.ext.azure.com/api/ Azure API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -module Az.Accounts | |
function Get-AzPortalToken { | |
<# | |
.SYNOPSIS | |
Refreshes the API token if required | |
.OUTPUTS | |
API Token Object | |
.NOTES | |
Inspired by https://github.com/Azure/azure-powershell/issues/7525#issuecomment-432384270 | |
Stolen from https://github.com/JustinGrote/Az.PortalAPI/blob/master/Az.PortalAPI/Public/Get-Token.ps1 | |
#> | |
[CmdletBinding(SupportsShouldProcess)] | |
param ( | |
#Your Azure Context. This will be discovered automatically if you have already logged in with Connect-AzAccount | |
[Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext] | |
$Context = (Get-AzContext | Select-Object -first 1), | |
#The application resource principal you wish to request a token for. Defaults to the Undocumented Azure Portal API | |
#Common Examples: https://management.core.windows.net/ https://graph.windows.net/ | |
$Resource = '74658136-14ec-4630-ad9b-26e160ff0fc6' | |
) | |
if (-not $Context) { | |
Connect-AZAccount -ErrorAction Stop | |
$Context = (Get-AzContext | Select-Object -first 1) | |
} | |
[Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id, $null, "Never", $null, $Resource) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment