Skip to content

Get-AzureRmRoleAssignment | Access denied to the specified API version | when running as Service Principal #3723

@danielstocker

Description

@danielstocker

Cmdlet(s)

Get-AzureRmRoleAssignment

PowerShell Version 5.1.14393.693

Module Version

2.7.0 AzureRM.Profile
3.7.0 AzureRM.Resources

OS Version

10.0.14393.693

Description

When running as a Service Principal Get-AzureRmRoleAssignment yields a cloud exception.
This is due to a 401 coming from the graph API when Get-AzureRmRoleAssignment calls https://graph.windows.net//getObjectsByObjectIds?api-version=1.6-internal

Returned content:
{"odata.error":{"code":"Authentication_Unauthorized","message":{"lang":"en","value":"Access denied to the specified API version."}}}

This does not happen when doing the same call on api-version=1.6

Example run in the same context:


# Get an Oauth 2 access token based on client id, secret and tenant domain
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientId;client_secret=$ClientSecret}
$oauth = Invoke-RestMethod -Method Post -Uri ("https://login.windows.net/" + $TenantDomain + "/oauth2/token?api-version=1.0") -Body $body

$assignmentObject = $assignment.object  
$body = @"
{
  "objectIds": [
    "$assignmentObject"
  ],
  "includeDirectoryObjectReferences": true
}
"@
 
$authHeader = @{"Authorization"= $oauth.access_token;"Content-Type"="application/json";"ContentLength"=$body.length }
$url = "https://graph.windows.net/" + $TenantDomain + "/getObjectsByObjectIds?api-version=1.6"
$content = Invoke-WebRequest -Headers $authHeader -Uri $url -Method Post -Body $body 

Debug Output

Example request body:

{
"objectIds": [
<object Id's populated by Get-AzureRmRoleAssignment>
],
"includeDirectoryObjectReferences": true
}

Repro Script

Add-AzureRmAccount -ServicePrincipal -Credential $creds -TenantId $TenantId
Get-AzureRMRoleAssignment

Workaround (partial)

In some circumstances Set-AzureRMRoleAssignment can be run which will return a 409 if the role is already set in a certain context. 409 can be caught to substitute some of the Get-* behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions