该错误通常是由于在运行该命令的帐户中没有足够的权限而引起的。为了解决这个问题,需要在运行该命令的帐户中添加对SqlServer实例的适当权限。以下是一个示例代码,用来将当前活动目录管理员与SqlServer实例相关联:
$subscriptionId = "yourSubscriptionId" $resourceGroup = "yourResourceGroupName" $serverName = "yourSqlServerName" $tenantId = "yourTenantId" $appId = "yourServicePrincipalAppId" $password = "yourServicePrincipalPassword"
$context = (Connect-AzAccount -TenantId $tenantId -ServicePrincipal -Credential (New-Object System.Management.Automation.PSCredential $appId, (ConvertTo-SecureString $password -AsPlainText -Force))).Context
$SqlServer = Get-AzSqlServer -Name $serverName -ResourceGroupName $resourceGroup
$administrator = @{ "SignInName" = "yourAdminSignInName" "ObjectId" = (Get-AzAdUser -SignInName "yourAdminSignInName").Id } Set-AzSqlServerActiveDirectoryAdministrator -ResourceGroupName $resourceGroup -ServerName $serverName -InputObject $SqlServer -Administrator $administrator -TenantId $tenantId -SubscriptionId $subscriptionId -Verbose
请确保替换示例代码中的值,以便适合您的环境。