Last active
May 12, 2023 15:06
-
-
Save TomaszOledzki/3fd656d7127c14a0e54076d39a831679 to your computer and use it in GitHub Desktop.
This file contains 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found error in line #5, should be:
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;