子网XXX未启用或不属于指定虚拟网络

时间:2018-10-16 11:43:45

标签: azure azure-powershell azure-virtual-network azure-devtest-labs

我已经从此处使用模板创建了DTL- https://github.com/Azure/azure-devtestlab/blob/master/Samples/101-dtl-create-lab/azuredeploy.json

之后,我要更改子网并使用以下脚本创建P2S VPN-

$VNetName = "dtlinfratest2"
$RG = "infratest2"
$Location = "westeurope"
$MyP2SRootCertPubKeyBase64 = "XXXXXXX"

# each virtaul network is inside a dev test lab so below values can hold good for all cases.
# Note: This is going to fail if VM exists in the virtual network
$GWSubName = "GatewaySubnet"
$VNetPrefix1 = "10.0.0.0/16"
$SubPrefix = "10.0.0.0/24"
$GWSubPrefix = "10.0.200.0/26"
$VPNClientAddressPool = "132.16.201.0/24"
$GWName = "GateWay"
$GWIPName = "GateWayIP"
$GWIPconfName = "GateWayIPConfig"

$vnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $RG

$fesub = New-AzureRmVirtualNetworkSubnetConfig -Name $vnet.Subnets.name -AddressPrefix $SubPrefix
$gwsub = New-AzureRmVirtualNetworkSubnetConfig -Name $GWSubName -AddressPrefix $GWSubPrefix
$vn = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $RG -Location $Location -AddressPrefix $VNetPrefix1 -Subnet $fesub, $gwsub -Force

$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $GWSubName -VirtualNetwork $vn

$pip = New-AzureRmPublicIpAddress -Name $GWIPName -ResourceGroupName $RG -Location $Location -AllocationMethod Dynamic
$ipconf = New-AzureRmVirtualNetworkGatewayIpConfig -Name $GWIPconfName -Subnet $subnet -PublicIpAddress $pip

$p2srootcert = New-AzureRmVpnClientRootCertificate -Name "P2SVNETRootCertName" -PublicCertData $MyP2SRootCertPubKeyBase64
New-AzureRmVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG -Location $Location -IpConfigurations $ipconf -GatewayType Vpn -VpnType RouteBased -EnableBgp $false -GatewaySku Standard -VpnClientAddressPool $VPNClientAddressPool -VpnClientRootCertificates $p2srootcert 

我正在创建虚拟机,子网中没有任何问题,并且在预定义的时间过后,虚拟机即将到期,之后,我发现虚拟机在实验室内部创建失败。错误消息-

  

子网DtlInfraTest2Subnet未启用或不属于指定虚拟网络   /subscriptions/XXXXX/resourcegroups/infratest2/providers/microsoft.devtestlab/labs/infratest2/virtualnetworks/dtlinfratest2

我检查了实验室内的“网络”选项卡,发现“在虚拟机创建中使用”已关闭,除非我手动勾选绿色,否则无法创建VM。

enter image description here

我尝试搜索一个powershell命令,但是找不到。默认情况下,当我们使用模板创建虚拟机时,“在虚拟机中使用”会消失,但是当所有虚拟机自动过期时会关闭

2 个答案:

答案 0 :(得分:1)

  

我尝试搜索powershell命令,但是找不到。

请尝试使用以下命令将实验室子网的USE IN VIRTUAL MACHINE CREATION设置为Yes

$a = Get-AzureRmResource -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.DevTestLab/labs/virtualnetworks -ResourceName "<your DevTest Lab name>/<Vnet name>" -ApiVersion 2016-05-15
$labSubnet = $a.Properties.subnetOverrides | Where-Object {$_.labSubnetName -eq "your labsubnet name"} 
$labSubnet.useInVmCreationPermission = "Allow"
$a | Set-AzureRmResource -Force -ApiVersion 2016-05-15

enter image description here

签入门户网站

enter image description here

答案 1 :(得分:0)

该错误消息表示您需要启用子网才能创建VM。我遵循您链接的template和提供的脚本来成功创建DTL和P2S VPN和子网。这是运行脚本后的默认虚拟网络设置。您可以尝试单击下面的红色分区,以启用在虚拟机创建中使用

enter image description here

或在代码中包含 subnetOverrides 模板以启用子网。您可以获得A sample template

   "subnetOverrides": [
                            {
                                "name": "[parameters('existingSubnetName')]",
                                "resourceId": "[variables('existingSubnetId')]",
                                "useInVmCreationPermission": "Allow",
                                "usePublicIpAddressPermission": "Allow"
                            }
                        ]