Trying to call an API via powershell whilst logged into Azure interactively using powershell. I hve written the code below, but I am getting the error after running
ConnectAzAccount
first
ParentContainsErrorRecordException:
Line |
23 | $token = $profileClient.AcquireAccessToken($currentAzureContext.t …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "AcquireAccessToken" with "1" argument(s): "Invalid tenant id provided. You can locate your tenant id by following the instructions listed here: https://docs.microsoft.com/partner-center/find-ids-and-domain-names"
The powershell script is as follow
function Get-AzCachedAccessToken (){
$ErrorActionPreference = 'Stop'
if (-not (Get-Module Az.Accounts)){
Import-Module Az.Accounts
}
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Debug ("Getting access token for the tenant" + $currentAzureContext.Tenant.TenantId)
Write-Verbose ("Tenant: {0}" -f $currentAzureContext.Subscription.Name)
$token = $profileClient.AcquireAccessToken($currentAzureContext.tenant.TenantId)
$token.AccessToken
}
function Get-AzBearerToken()
{
$ErrorActionPreference = 'Stop'
('Bearer {0}' -f (Get-AzCachedAccessToken))
}
#GET https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/$rg/providers/Microsoft.ApiManagement/service/apimService1/tenant/gitAccess?api-version=2024-05-01