New-AzureRmRoleAssignment problem with Azure Service Principal
Hi I'm using Powershell to Assign Roles to users. When I use Login-AzureRmAccount all goes well but when I try with Connect-AzureRmAccount -servicePrincipal I got the error
The servic Prinicipal is Owner of the subscription and have all permissions on Graph API
Thanks for help
New-AzureRmRoleAssignment : The provided information does not map to an AD object id.
At C:\Users\yassir\Documents\CloudToolBox\scripts\set-RolesFromJson.ps1:44 char:5
+ New-AzureRmRoleAssignment -SignInName "xxxxxxx. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmRoleAssignment], KeyNotFoundException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand
----------
Powershell
$AzureRMAccount = Connect-AzureRmAccount -Credential $credentials -ServicePrincipal -TenantId $tenant_id
if ($AzureRMAccount){
$subscription = $params.subscriptionId
Set-AzureRmContext -SubscriptionId $subscription -TenantId $tenant_id
New-AzureRmRoleAssignment -SignInName "AUSER" -RoleDefinitionName "Contributor" -scope "/subscriptions/xxxxx/resourceGroups/xxxxx"
}
@yassirkachri Can you turn on $DebugPreference="Continue" before runnign this script 0- this will give us the requests and responses with the server and help to diagnose what the issue might be. The error message indicates that the SignInName was not found, but the server response messages will tell us for sure.
After $DebugPreference="Continue" I have more information and it seems that the problem is here 👍 `
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
DEBUG: 04/12/2018 07:22:30: - TokenCache: Serializing token cache with 2 items.
DEBUG: ============================ HTTP REQUEST ============================
HTTP Method:
GET
Absolute Uri:
https://graph.windows.net/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/users?$filter=userPrincipalName eq 'AUSER'&api-version=1.6
Headers:
x-ms-client-request-id : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
accept-language : en-US
Body:
DEBUG: ============================ HTTP RESPONSE ============================
Status Code:
Forbidden
Headers:
Pragma : no-cache
ocp-aad-diagnostics-server-name: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
request-id : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client-request-id : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
x-ms-dirapi-data-contract-version: 1.6
ocp-aad-session-key : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X-Content-Type-Options : nosniff
DataServiceVersion : 3.0;
Strict-Transport-Security : max-age=31536000; includeSubDomains
Access-Control-Allow-Origin : *
Duration : 584292
Cache-Control : no-cache
Date : Thu, 12 Apr 2018 07:22:30 GMT
Server : Microsoft-IIS/10.0
X-AspNet-Version : 4.0.30319
X-Powered-By : ASP.NET
Body:
{
"odata.error": {
"code": "Authorization_RequestDenied",
"message": {
"lang": "en",
"value": "Insufficient privileges to complete the operation."
}
}
}
New-AzureRmRoleAssignment : The provided information does not map to an AD object id.
At C:\Users\yassir\Documents\CloudToolBox\scripts\set-RolesFromJson.ps1:44 char:5
+ New-AzureRmRoleAssignment -SignInName "AUSER. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmRoleAssignment], KeyNotFoundException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand
DEBUG: AzureQoSEvent: CommandName - New-AzureRmRoleAssignment; IsSuccess - False; Duration - 00:00:00.3611205; Exception - System.Collections.Generic.KeyNotFoundException: The provided information does not map to an AD object
id.
at Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory.ActiveDirectoryClient.GetObjectId(ADObjectFilterOptions options)
at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient.CreateRoleAssignment(FilterRoleAssignmentsOptions parameters)
at Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord();
DEBUG: Finish sending metric.
DEBUG: 9:22:31 AM - NewAzureRoleAssignmentCommand end processing.
DEBUG: 9:22:31 AM - NewAzureRoleAssignmentCommand end processing.
----------
@yassirkachri does the service principal you are signing in with have the appropriate permissions to assign roles in the given scope? Based on the returned message (Insufficient privileges to complete the operation), it would seem that the service principal itself doesn't have the correct permissions to assign out roles.
CC: @darshanhs90
the Service Principal is Owner of the subscription and have all permissions

@yassirkachri What are the permissions the service prinpal has under graph for the associated tenant? The 'application permissions' will need to include the ability to list directory contents and view user data.
@markcowl can you give us the exact permissions needed please ?
@YoannGUILLO You would need to have Read and rite directory data permissions within the tenant
@markcowl where can I see the service principal permissions under graph ?
These are 'AzureAD Graph' permissions, not Microsoft.Graph permissions. They should be listed as 'Application Permissions' under 'Windows Azure Active Directory '
Still the same error that @yassirkachri, I have a service principal used as a run as account to execute my runbooks on Automation, my runbook execute the command New-AzureRmRoleAssignment to apply a specific user as reader on a resource group.
My service principal is owner of my subscription and permissions below has been applied :
When the runbook is executed this error appear :
The provided information does not map to an AD object id. at Microsoft.Azure.Commands.Resources.Models.ActiveDirectory.ActiveDirectoryClient.GetObjectId(ADObjectFilterOptions options) at Microsoft.Azure.Commands.Resources.Models.Authorization.AuthorizationClient.CreateRoleAssignment(FilterRoleAssignmentsOptions parameters) at Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand.ExecuteCmdlet() at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
@cormacpayne Can you take a look at this issue?
I am also running into this same issue. When running locally the command works fine, but when I connect to Azure using the following:
$ServicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Add-AzureRmAccount
-ServicePrincipal
-TenantId $ServicePrincipalConnection.TenantId
-ApplicationId $ServicePrincipalConnection.ApplicationId
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint | Write-Verbose
I receive the New-AzureRmRoleAssignment : The provided information does not map to an AD object id. error.
Error resolved, I updated the modules under my Azure Automation Account.
For information it works with just "Read directory data"
@YoannGUILLO - Can you post an image of your modules and their versions?
Closing as resolved.