Skip to content

Instantly share code, notes, and snippets.

@TomaszOledzki
Last active May 12, 2023 15:06
Show Gist options
  • Save TomaszOledzki/3fd656d7127c14a0e54076d39a831679 to your computer and use it in GitHub Desktop.
Save TomaszOledzki/3fd656d7127c14a0e54076d39a831679 to your computer and use it in GitHub Desktop.
Function GetToken {
Try {
Write-Verbose "START: Function GetToken";
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;
if(-not $azProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this Function.";
Exit;
}
$currentAzureContext = Get-AzContext;
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azProfile);
Write-Debug ("Getting access token for tenant: " + $($currentAzureContext.Tenant.TenantId));
$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId);
Write-Verbose "DONE: Function GetToken";
Return $token.AccessToken;
} Catch {
Write-Verbose "ERROR: Function GetToken";
Throw $PSItem;
}
}
@TomaszOledzki
Copy link
Author

Found error in line #5, should be:
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment