我尝试使用我的应用程序 ID 和客户端秘密令牌登录 PowerShell。但我在以下内容中收到错误消息。我应该怎么做才能让它有足够的权限?
我的脚本:
$password = ConvertTo-SecureString "client secret" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "application ID", $password
Write-Output "Login to azure environment"
Connect-AzAccount -Credential $cred -Tenant "tenant ID" -Subscription "subscription ID" -ServicePrincipal
Write-Output "Restarting Web App..."
Restart-AzWebApp -ResourceGroupName "testconfogproxy_group" -Name "testconfogproxy"
Write-Output "Operation Completed."
错误信息。
Connect-AzAccount : The provided account adf74536-2bd3-456a-96ba-2f98e4902525 does not have access to subscription ID "
d9c408ef-39c7-408a-97c7-68c6ae84b760". Please try logging in with different credentials or a different subscription ID.
If a subscription is not specified, please check the configs by `Get-AzConfig`.
检查服务主体在自动化帐户和 Web 应用程序下是否具有正确的
contributor
角色。
此外,请转到相应的路径,查看是否为自动化帐户启用了
system managed identity
并为相关服务主体提供了自动化 contributor
角色。
Automation account >> Access control >> Add a role assignment
对于订阅角色的 Web 应用程序,请执行相同的操作。
如果问题仍然存在,请将适当的
Contributor or Owner
角色分配给与应用程序 ID 关联的服务主体。
提供必要的权限后,我能够成功执行以下脚本,如图所示。
$clientsecret="xxx"
$appid="xxxx"
$tenantid="xxxx"
$subscriptionid="xxxx"
$password = ConvertTo-SecureString $clientsecret -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $appid, $password
Write-Output "Login to azure environment"
Connect-AzAccount -Credential $cred -Tenant $tenantid -Subscription $subscriptionid -ServicePrincipal
Write-Output "Restarting Web App..."
Restart-AzWebApp -ResourceGroupName "xxxx" -Name "newapj"
Write-Output "Operation Completed."
输出:
我还尝试从 Azure PowerShell 运行脚本。要从 PowerShell 登录并重新启动 Web 应用程序,您还需要向应用程序 ID 提供
website contributor
角色,以便使用服务主体访问它。
转到以下路径:
Webapp >> Access control >> Add a role assignment >> website contributor >> select your service principal app id