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; | |
} | |
} |
Hi TomaszOledzki,
I am getting the below error when I am executing your code.
`Unable to find type [Microsoft.Azure.Commands.Common.Authentication.Abstractions.azProfileProvider].
At line:1 char:14* ... azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstracti ... * ``` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` * CategoryInfo : InvalidOperation: (Microsoft.Azure...ProfileProvider:TypeName) [], RuntimeException * FullyQualifiedErrorId : TypeNotFound`
I have already installed Az modules, and I searched a lot on google about 'azProfileProvider', except for your links I am getting nothing. So please help me to let know what else required to get it to work?
Hi Ashish-Jovial,
Which version of "PowerShell", "Az module" and "Az.Profile" are you using?
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
Hi TomaszOledzki,
I am getting the below error when I am executing your code.
`Unable to find type [Microsoft.Azure.Commands.Common.Authentication.Abstractions.azProfileProvider].
At line:1 char:14
I have already installed Az modules, and I searched a lot on google about 'azProfileProvider', except for your links I am getting nothing. So please help me to let know what else required to get it to work?