无法通过PowerShell分配IAM角色

时间:2018-03-14 12:38:59

标签: azure access-control azure-powershell

主要问题是,我可以使用Azure门户分配IAM角色,但在通过PowerShell尝试相同时遇到错误。 这是门户网站操作的结果: enter image description here

当我尝试通过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

任何想法在哪里寻找错误?

1 个答案:

答案 0 :(得分:1)

请使用此脚本获取用户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"

有关命令New-AzureRmRoleAssignment的详细信息,请参阅此article

希望这有帮助。