请记住,Azure AD 门户中显示的“清单”只不过是应用程序对象的轻度约束表示,由 Azure AD Graph API 公开:https://msdn.microsoft.com/Library/Azure/Ad/Graph/api/entity-and-complex-type-reference#application-entity
Azure AD PowerShell(AzureAD 模块)只是同一 API 的简单包装。 New‑AzureADApplication 对/applications 执行POST,Get‑AzureADApplication 执行GET,Set‑AzureADApplication 执行PATCH,Remove‑AzureADApplication 执行DELETE。
因此,请记住这一点,请考虑以下输入文件 app-roles.json:
[
{
"allowedMemberTypes": [ "Application" ],
"description": "Read some things in the My App service",
"displayName": "Read some things",
"id": "b2b2e6de-bb42-41b4-92db-fda89218b5ae",
"isEnabled": true,
"value": "Things.Read.Some"
},
{
"allowedMemberTypes": [ "User" ],
"description": "Super admin role for My App",
"displayName": "My App Super Admin",
"id": "a01eca9b-0c55-411d-aa5f-d8cfdbadf500",
"isEnabled": true,
"value": "super_admin"
}
]
您可以使用以下脚本在应用上设置这些应用角色(请注意,这将删除任何现有的应用角色,这将导致错误,因为它们之前没有被禁用):
$appId = "{app-id}"
$pathToAppRolesJson = "app-roles.json"
# Read all desired app roles from JSON file
$appRolesFromJson = Get-Content -Path $pathToAppRolesJson -Raw | ConvertFrom-Json
# Build a new list of Azure AD PowerShell AppRole objects
$appRolesForApp = @()
$appRolesFromJson | ForEach-Object {
# Create new Azure AD PowerShell AppRole object for each app role
$appRole = New-Object "Microsoft.Open.AzureAD.Model.AppRole"
$appRole.AllowedMemberTypes = $_.allowedMemberTypes
$appRole.Description = $_.description
$appRole.DisplayName = $_.displayName
$appRole.Id = $_.id
$appRole.IsEnabled = $_.isEnabled
$appRole.Value = $_.value
# Add to the list of app roles
$appRolesForApp += $appRole
}
# Update the Application object with the new list of app roles
$app = Get-AzureADApplication -Filter ("appId eq '{0}'" -f $appId)
Set-AzureADApplication -ObjectId $app.ObjectId -AppRoles $appRolesForApp