创建用户时出错:执行新用户代码时出错:Authorization_RequestDenied 消息:权限不足,无法完成操作

问题描述 投票:0回答:1

我需要你的帮助我正在尝试管理 azure 自动化的身份配置我正在使用 azure 自动化 runbook 和系统管理的身份自动化将用户添加到 Azure AD 的过程不工作,并向我显示这个我无法解决的错误。请帮我解决这个问题。这是我的 powershell 代码和错误: 错误: 创建用户时出错:执行 NewUser 时出错 代码:Authorization_RequestDenied 消息:权限不足,无法完成操作。 RequestId: 34be9739-4e8a-47f1-b5b3-fa2adb042d8e 日期时间戳:2023 年 4 月 13 日星期四 09:15:30 GMT HttpStatusCode:禁止访问 HttpStatusDescription: 禁止访问 HttpResponseStatus:已完成

代码:

参数( [参数(强制=$true)] [字符串]$DisplayName,

[Parameter(Mandatory=$true)]
[string]$UserPrincipalName,

[Parameter(Mandatory=$true)]
[string]$MailNickName,

[Parameter(Mandatory=$true)]
[string]$UsageLocation,

[Parameter(Mandatory=$true)]
[string]$Department,

[Parameter(Mandatory=$true)]
[string]$ConnectionName

)

尝试{

# Connect to Azure with the system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context



Import-Module AzureAD
#Connect-AzureAD
$azureContext = Connect-AzAccount -Identity
$azureContext = Set-AzContext -SubscriptionName $azureContext.context.Subscription -DefaultProfile $azureContext.context
$graphToken = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/"
$aadToken = Get-AzAccessToken -ResourceUrl "https://graph.windows.net"
Connect-AzureAD -AccountId $azureContext.account.id -TenantId $azureContext.tenant.id -AadAccessToken $aadToken.token -MsAccessToken $graphToken.token    

$PasswordProfile = New-Object Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = "MotDePasse123!"
$PasswordProfile.ForceChangePasswordNextLogin = $false
$User = New-AzureADUser -AccountEnabled $true `
    -DisplayName $DisplayName `
    -UserPrincipalName $UserPrincipalName `
    -MailNickName $MailNickName `
    -UsageLocation "US" `
    -Department $Department `
    -PasswordProfile $PasswordProfile


Write-Output "User $UserPrincipalName created successfully."

}赶上{ 写输出“创建用户时出错:$_” }

我正在使用具有托管标识的 azure 自动化 runbook 在 azure ad 中创建一个新用户,我知道这两个用户都具有一般管理员角色

azure-active-directory azure-automation azure-runbook identity-management
1个回答
0
投票

我试图在我的环境中重现相同的内容并得到如下相同的错误:

enter image description here

如果托管身份没有足够的角色来执行操作,通常会发生错误。

创建新用户

Administrator role
是必需的。确保分配角色。

分配角色后,我可以像下面这样创建一个新用户:

#Connect to Azure with the system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context

Import-Module AzureAD
#Connect-AzureAD
$azureContext = Connect-AzAccount -Identity
$azureContext = Set-AzContext -SubscriptionName $azureContext.context.Subscription -DefaultProfile $azureContext.context
$graphToken = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/"
$aadToken = Get-AzAccessToken -ResourceUrl "https://graph.windows.net"
Connect-AzureAD -AccountId $azureContext.account.id -TenantId $azureContext.tenant.id -AadAccessToken $aadToken.token -MsAccessToken $graphToken.token    

$PasswordProfile = New-Object Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = "MotDePasse123!"
$PasswordProfile.ForceChangePasswordNextLogin = $false
$User = New-AzureADUser -AccountEnabled $true `
    -DisplayName $DisplayName `
    -UserPrincipalName $UserPrincipalName `
    -MailNickName $MailNickName `
    -UsageLocation "US" `
    -Department $Department `
    -PasswordProfile $PasswordProfile


Write-Output "User $UserPrincipalName created successfully."

enter image description here

在门户中,用户创建成功如下所示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.