无法通过 PowerShell 分配 IAM 角色
Cannot assign an IAM role via PowerShell
主要问题是,我可以使用 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"
更多关于命令New-AzureRmRoleAssignment
的信息,请参考这个article。
希望这对您有所帮助。
主要问题是,我可以使用 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"
更多关于命令New-AzureRmRoleAssignment
的信息,请参考这个article。
希望这对您有所帮助。