首页 > 解决方案 > Azure Powershell - 无法将子网设置添加到数据库

问题描述

我正在尝试使用 Azure Az 模块向 SQL Server 添加子网。我正在使用的命令是

New-AzSqlServerVirtualNetworkRule -VirtualNetworkRuleName "newvnetrule1" -ServerName $sqlServer.ServerName -ResourceGroupName $sqlServer.ResourceGroupName -VirtualNetworkSubnetId $newsubnetId -ErrorAction Stop

我得到一个例外说:

The client with object id does not have permission to perform this action

对象 id 属于 name 的 SPN Azure SQL Virtual Network to Network Resource Provider

我在使用 ARM 模板配置 cosmos db 帐户时遇到了完全相同的问题,只是这次错误的 SPN 是Azure Cosmos DB Virtual Network to Network Resource Provider

任何人都可以对此有所了解吗?以前可以正常工作的相同代码。所有服务也都为子网注册

标签: azurepowershellazure-virtual-networksubnet

解决方案


角色就够了,Owner我这边测试一下,效果很好。

$virtualNetworkSubnetId = "/subscriptions/xxxxxxx/resourceGroups/joynet/providers/Microsoft.Network/virtualNetworks/joysqlnet/subnets/default"

New-AzSqlServerVirtualNetworkRule -ResourceGroupName joynet -ServerName joyser -VirtualNetworkRuleName vnetrule1 -VirtualNetworkSubnetId $virtualNetworkSubnetId

在此处输入图像描述


要解决此问题,请尝试使用Clear-AzContext清除所有本地帐户信息,然后使用以下脚本重新登录。

$azureAplicationId ="<Application ID>"
$azureTenantId= "<Tenant ID>"
$azurePassword = ConvertTo-SecureString "<Client secret>" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal

然后运行(Get-AzContext).Account,确保与您正在使用的服务主体的Id相同,服务主体的也应与中的 GUID 相同。Application IDTenant IDTenants

在此处输入图像描述

在此处输入图像描述


推荐阅读