在 Azure Active Directory 中使用 PowerShell 以資源範圍指派自訂角色
本文說明如何在 Azure Active Directory (Azure AD) 的全組織範圍內建立角色指派。 在整個組織範圍指派角色,會授與跨 Azure AD 組織的存取權。 若要建立具有單一 Azure AD 資源範圍的角色指派,請參閱如何建立自訂角色,並在資源範圍中指派該角色。 本文使用 Azure Active Directory PowerShell 第 2 版模組。
如需 Azure AD 角色的詳細資訊,請參閱 Azure AD 內建角色。
必要條件
- Azure AD Premium P1 或 P2 授權
- 特殊權限角色管理員或全域管理員
- 使用 PowerShell 時的 AzureADPreview 模組
如需詳細資訊,請參閱使用 PowerShell 或 Graph 總管的必要條件。
使用資源範圍將目錄角色指派給使用者或服務主體
- 載入 Azure AD PowerShell (預覽) 模組。
- 執行命令
Connect-AzureAD
以登入。 - 使用下列 PowerShell 指令碼建立新的角色。
## Assign a role to a user or service principal with resource scope
# Get the user and role definition you want to link
$user = Get-AzureADUser -Filter "userPrincipalName eq 'cburl@f128.info'"
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
# Get app registration and construct resource scope for assignment.
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
$directoryScope = '/' + $appRegistration.objectId
# Create a scoped role assignment
$roleAssignment = New-AzureADMSRoleAssignment -DirectoryScopeId $directoryScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
若要將角色指派給服務主體,而不是使用者,請使用 AzureADMSServicePrincipal Cmdlet。
角色定義
角色定義物件包含內建或自訂角色的定義,以及該角色指派所授與的權限。 此資源會顯示自訂角色定義和內建目錄角色 (以 roleDefinition 等同的表單來顯示)。 現今 Azure AD 的組織最多可以定義 30 個唯一的自訂角色定義。
建立角色定義
# Basic information
$description = "Can manage credentials of application registrations"
$displayName = "Application Registration Credential Administrator"
$templateId = (New-Guid).Guid
# Set of actions to include
$rolePermissions = @{
"allowedResourceActions" = @(
"microsoft.directory/applications/standard/read",
"microsoft.directory/applications/credentials/update"
)
}
# Create new custom directory role
$customAdmin = New-AzureADMSRoleDefinition -RolePermissions $rolePermissions -DisplayName $displayName -Description $description -TemplateId $templateId -IsEnabled $true
讀取和列出角色定義
# Get all role definitions
Get-AzureADMSRoleDefinitions
# Get single role definition by ID
Get-AzureADMSRoleDefinition -Id 86593cfc-114b-4a15-9954-97c3494ef49b
# Get single role definition by templateId
Get-AzureADMSRoleDefinition -Filter "templateId eq 'c4e39bd9-1100-46d3-8c65-fb160da0071f'"
更新角色定義
# Update role definition
# This works for any writable property on role definition. You can replace display name with other
# valid properties.
Set-AzureADMSRoleDefinition -Id c4e39bd9-1100-46d3-8c65-fb160da0071f -DisplayName "Updated DisplayName"
刪除角色定義
# Delete role definition
Remove-AzureADMSRoleDefinitions -Id c4e39bd9-1100-46d3-8c65-fb160da0071f
角色指派
角色指派包含將指定安全性主體 (使用者或應用程式服務主體) 連結至角色定義的資訊。 如有需要,您可以為指派的權限新增單一 Azure AD 資源的範圍。 內建和自訂角色都支援限制角色指派的範圍。
建立角色指派
# Get the user and role definition you want to link
$user = Get-AzureADUser -Filter "userPrincipalName eq 'cburl@f128.info'"
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
# Get app registration and construct resource scope for assignment.
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
$directoryScope = '/' + $appRegistration.objectId
# Create a scoped role assignment
$roleAssignment = New-AzureADMSRoleAssignment -DirectoryScopeId $directoryScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
讀取並列出角色指派。
# Get role assignments for a given principal
Get-AzureADMSRoleAssignment -Filter "principalId eq '27c8ca78-ab1c-40ae-bd1b-eaeebd6f68ac'"
# Get role assignments for a given role definition
Get-AzureADMSRoleAssignment -Filter "roleDefinitionId eq '355aed8a-864b-4e2b-b225-ea95482e7570'"
移除角色指派
# Remove role assignment
Remove-AzureADMSRoleAssignment -Id 'qiho4WOb9UKKgng_LbPV7tvKaKRCD61PkJeKMh7Y458-1'
後續步驟
- 您可在 Azure AD 系統管理角色論壇上與我們共享資訊。
- 如需角色和 Azure AD 管理員角色指派的詳細資訊,請參閱指派管理員角色
- 如需預設使用者權限,請參閱預設來賓和成員使用者權限的比較