# Basic role information
$description = "Can manage user and group assignments for Applications"
$displayName = "Manage user and group assignments"
$templateId = (New-Guid).Guid
# Set of permissions to grant
$allowedResourceAction = @("microsoft.directory/servicePrincipals/appRoleAssignedTo/update")
$rolePermission = @{'allowedResourceActions'= $allowedResourceAction}
$rolePermissions = $rolePermission
# Create new custom admin role
$customRole = New-MgRoleManagementDirectoryRoleDefinition -Description $description `
-DisplayName $displayName -RolePermissions $rolePermissions -TemplateId $templateId -IsEnabled
指派自訂角色
使用此 PowerShell 指令碼來指派角色。
# Get the user and role definition you want to link
$user = Get-MgUser -Filter "userPrincipalName eq 'chandra@example.com'"
$roleDefinition = Get-MgRoleManagementDirectoryRoleDefinition -Filter "displayName eq 'Manage user and group assignments'"
# Get app registration and construct scope for assignment.
$appRegistration = Get-MgApplication -Filter "displayName eq 'My Filter Photos'"
$directoryScope = '/' + $appRegistration.Id
# Create a scoped role assignment
$roleAssignment = New-MgRoleManagementDirectoryRoleAssignment -DirectoryScopeId $directoryScope `
-PrincipalId $user.Id -RoleDefinitionId $roleDefinition.Id
POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleDefinitions
{
"description": "Can manage user and group assignments for Applications.",
"displayName": "Manage user and group assignments",
"isEnabled": true,
"rolePermissions":
[
{
"allowedResourceActions":
[
"microsoft.directory/servicePrincipals/appRoleAssignedTo/update"
]
}
],
"templateId": "<PROVIDE NEW GUID HERE>",
"version": "1"
}
使用 Microsoft Graph API 指派自訂角色
使用建立 unifiedRoleAssignment API 指派自訂角色。 角色指派結合了安全性主體識別碼 (可以是使用者或服務主體)、角色定義識別碼,以及 Microsoft Entra 資源範圍。 如需角色指派元素的詳細資訊,請參閱自訂角色概觀
POST https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments
{
"@odata.type": "#microsoft.graph.unifiedRoleAssignment",
"principalId": "<PROVIDE OBJECTID OF USER TO ASSIGN HERE>",
"roleDefinitionId": "<PROVIDE OBJECTID OF ROLE DEFINITION HERE>",
"directoryScopeId": "/"
}