Skip to content

Instantly share code, notes, and snippets.

@TomaszOledzki
Last active May 12, 2023 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
}
@Ashish-Jovial
Copy link

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?

@TomaszOledzki
Copy link
Author

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?

@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