az role assignment create --assignee <`appId`> --role <Role> --scope <resource URI>
以下命令行界面示例将 Reader 角色分配给 rg-001 资源组中所有资源的服务主体:
az role assignment create --assignee 00001111-aaaa-2222-bbbb-3333cccc4444 --role Reader --scope '\/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/rg-001'
$subscriptionId = "{azure-subscription-id}"
$resourceGroupName = "{resource-group-name}"
# Authenticate to a specific Azure subscription.
Connect-AzAccount -Environment AzureChinaCloud -SubscriptionId $subscriptionId
# Password for the service principal
$pwd = "{service-principal-password}"
$secureStringPassword = ConvertTo-SecureString -String $pwd -AsPlainText -Force
# Create a new Azure Active Directory application
$azureAdApplication = New-AzADApplication `
-DisplayName "My Azure Monitor" `
-HomePage "https://localhost/azure-monitor" `
-IdentifierUris "https://localhost/azure-monitor" `
-Password $secureStringPassword
# Create a new service principal associated with the designated application
New-AzADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
# Assign Reader role to the newly created service principal
New-AzRoleAssignment -RoleDefinitionName Reader `
-ServicePrincipalName $azureAdApplication.ApplicationId.Guid
后续步骤
需使用访问控制 (IAM) 将应用分配到你想要访问的资源的角色,才能使用应用、客户端 ID 和机密生成令牌。 角色将取决于要使用的资源类型和 API。
例如,