我有一个在 Azure 中运行的管道,它使用以下命令运行 Azure powershell 任务。现在,我已经在我的powershell控制台上对此进行了测试,它运行得很好。从表面上看,它看起来像是一个权限问题,但令我困惑的是数据库创建工作正常,不起作用的是它所在的部分运行下面的代码。
$数据库|设置-AzSqlServerActiveDirectoryAdministrator -DisplayName“xxxxx”
完整的内联脚本
$my_rg = "xxxx"
$my_db = "xxx"
$my_server = "xxx"
$database = New-AzSqlDatabase -ResourceGroupName $my_rg `
-ServerName $my_server `
-DatabaseName $my_db `
-RequestedServiceObjectiveName "S1" `
-Edition "Standard"
$database | Set-AzSqlServerActiveDirectoryAdministrator -DisplayName "xxxxx"
管道抛出错误。
========================== Starting Command Output ===========================
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\17384b06-bf34-4f38-a0b3-8ade4268c4e6.ps1'"
Import-Module -Name C:\Modules\az_3.1.0\Az.Accounts\2.2.5\Az.Accounts.psd1 -Global
WARNING: Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the
same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can
use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure
Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found
here: https://aka.ms/azps-migration-guide
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
Clear-AzContext -Scope Process
Connect-AzAccount -ServicePrincipal -Tenant xxxxxxx -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
Set-AzContext -SubscriptionId xxxx -TenantId xxxxxxxxxxx
##[error]Operation returned an invalid status code 'Forbidden'
##[error]PowerShell exited with code '1'.
最佳答案
##[error]Operation returned an invalid status code 'Forbidden'
根据错误消息,问题的原因是服务连接无权设置 Azure SQL 服务器的管理员。
在 Azure DevOps 中,服务连接将在 Azure 门户中创建服务主体,然后使用服务主体的权限来访问 Azure 资源。
要解决此问题,您可以将 Azure SQL 服务器所有者角色授予服务主体。
步骤如下:
第 1 步:在 Azure 门户 -> Azure Active Directory -> 应用注册中检查服务主体。
第 2 步:在 Azure SQL Server -> 访问控制中授予服务主体所有者角色
关于azure - ##[错误]操作返回无效状态代码 'Forbidden' 错误 Azure 管道运行 Set-AzSqlServerActiveDirectoryAdministrator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73563031/