Azure Runbook 无法修改 Azure AD 应用程序

Azure Runbook can't modify Azure AD application

我正在尝试在 Azure 自动化 runbook 中执行此操作

$app = Get-AzureADApplication -ObjectId $ApplicationId
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
$appRole.AllowedMemberTypes.Add("User");
$appRole.DisplayName = $TenantName + " Users"
$appRole.Id = New-Guid
$appRole.IsEnabled = $true
$appRole.Description = "Users of the tenant"
$appRole.Value = $TenantName

$app.AppRoles.Add($appRole)

Set-AzureADApplication -ObjectId $ApplicationId -AppRoles $app.AppRoles

读取应用程序工作正常,当我打印应用程序变量时,我可以看到它是正确的应用程序。从我自己的机器上执行脚本也没有错误。然而通过运行手册执行它给我:

Set-AzureADApplication : Error occurred while executing SetApplication 
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed

至此,我已将 Active Directory 的所有权限授予 Azure AD 中的自动化应用程序注册 API。我也点击了 "Grant Permissions"。我知道这是正确的应用程序注册,因为当我在开始工作的 "Graph Api" 上授予正确的权限时,脚本还会邀请外部用户。

我在 运行 一本书中试用了您的确切脚本并使其工作,我不得不将代码添加到 "Login as the service principal" 就在您的 PowerShell 脚本之前。您可以在此处查看更多详细信息: Using Azure Run As Account in Azure Automation

在权限方面,我只授予了 1 个应用程序权限(即 "Read and write all applications"),然后单击 "Grant Permissions",因为它确实需要管理员同意。这些步骤是由我的 Azure AD 中具有 "Global administrator" 目录角色的用户完成的。

这是我最终的 PowerShell 脚本(从编辑 运行书中复制):

# Get Azure Run As Connection Name
$connectionName = "AzureRunAsConnection"
# Get the Service Principal connection details for the Connection name
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

# Logging in to Azure AD with Service Principal
"Logging in to Azure AD..."
Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

$ApplicationId = "redacted-xxxx-xxxx-xxxx-xxxxxxxe3"
$TenantName = "RohitTenant"
$app = Get-AzureADApplication -ObjectId $ApplicationId
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
$appRole.AllowedMemberTypes.Add("User");
$appRole.DisplayName = $TenantName + " Users"
$appRole.Id = New-Guid
$appRole.IsEnabled = $true
$appRole.Description = "Users of the tenant"
$appRole.Value = $TenantName
$app.AppRoles.Add($appRole)

Set-AzureADApplication -ObjectId $ApplicationId -AppRoles $app.AppRoles

以下是我遵循的其他一些重要步骤的屏幕截图,您可能已经完成也可能没有完成。

  1. 在创建自动化帐户时创建 Azure 运行 作为帐户

  2. 确保您的自动化帐户的帐户设置现在有 运行 作为帐户。

  3. 找到为 运行 创建的 App Registration 作为 Account 并授予其读取和写入所有 Azure AD 应用程序的权限。