Azure 无法通过PowerShell分配IAM角色

Azure 无法通过PowerShell分配IAM角色,azure,access-control,azure-powershell,Azure,Access Control,Azure Powershell,主要问题是,我可以使用Azure门户分配IAM角色,但在通过PowerShell尝试相同操作时出错。 这是门户操作的结果: 当我尝试通过PowerShell执行相同操作时,收到以下错误: > New-AzureRmRoleAssignment -ObjectId "d585d0b6-eb2b-4d7c-99b4-7c357219f78d" -RoleDefinitionName "Reader" -ResourceName "datalaketestmh" -ResourceType "

主要问题是,我可以使用Azure门户分配IAM角色,但在通过PowerShell尝试相同操作时出错。 这是门户操作的结果:

当我尝试通过PowerShell执行相同操作时,收到以下错误:

> New-AzureRmRoleAssignment -ObjectId "d585d0b6-eb2b-4d7c-99b4-7c357219f78d" -RoleDefinitionName "Reader" -ResourceName "datalaketestmh" -ResourceType "Microsoft.DataLakeStore/accounts" -ResourceGroupName "My-Test-Resource-Group"
New-AzureRmRoleAssignment : Principal d585d0b6eb2b4d7c99b47c357219f78d does not exist in the directory 3596192b-fdf5-4e2c-a6fa-acb706c963d8.
At line:1 char:1
+ New-AzureRmRoleAssignment -ObjectId "d585d0b6-eb2b-4d7c-99b4-7c357219 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [New-AzureRmRoleAssignment], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand

想知道在哪里查找错误吗?

请使用此脚本获取用户ID:

$a = Get-AzureRmADUser | ?{ $_.UserPrincipalName -eq 'username@xxxx.onmicrosoft.com' } | select id
$userid = $a.id.Guid
然后使用
$userid
分配角色:

New-AzureRmRoleAssignment -ObjectId $userid -RoleDefinitionName "Reader" -ResourceName "datalaketestmh" -ResourceType "Microsoft.DataLakeStore/accounts" -ResourceGroupName "My-Test-Resource-Group"
顺便说一下,请检查您的Azure PowerShell版本,我的Azure PowerShell版本是
5.1.1
,该脚本适合我:

PS C:\Users\jason> Get-Module -ListAvailable -Name Azure -Refresh


    Directory: C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.1.1      Azure                               {Get-AzureAutomationCertificate, Get-AzureAutomationConnection, New-AzureAutomationConnection, Remove-AzureAutomationConnection...}
您还可以在
ObjectId
的内部使用SignInName,如下所示:

New-AzureRmRoleAssignment -SignInName john.doe@contoso.com -RoleDefinitionName Owner -Scope "/subscriptions/86f81fc3-b00f-48cd-8218-3879f51ff362/resourcegroups/rg1/providers/Microsoft.Web/sites/site1"
有关命令
新azurermroleasignment
的更多信息,请参阅此


希望这有帮助。

您是否正在尝试将角色分配给用户?如果是,请尝试使用不必要的
SignInName
参数,但这可能更容易为您所用?如果您需要更多帮助,请告诉我:)它起作用了,但搜索用户更快:
Get AzureRmADUser-UserPrincipalName'username@xxxx.onmicrosoft.com“