为什么天蓝色的子网没有subnet.id参数

时间:2019-06-09 13:42:10

标签: azure powershell virtual-machine azure-powershell subnet

我有以下简单的代码可以生成3个子网:

$resourceGroupName = "Test1"
$location = "West Europe"
$VNetName = "Test-VM"
$VNetIPRange = "192.168.0.0/21"
$SubnetAName = "SubA"
$SubnetAIPRange = "192.168.0.0/24"
$SubnetBName = "SubB"
$SubnetBIPRange = "192.168.1.0/24"
$SubnetCName = "SubC"
$SubnetCIPRange = "192.168.2.0/24"

New-AzResourceGroup -name $resourceGroupName -Location $location -Verbose

# subnets & vnet
$subnets = New-AzVirtualNetworkSubnetConfig `
    -Name $SubnetAName `
    -AddressPrefix $SubnetAIPRange `
    -Verbose
$vnet = New-AzVirtualNetwork `
    -Name $VNetName `
    -AddressPrefix $VNetIPRange `
    -Subnet $subnets `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -Verbose
Add-AzVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name $SubnetBName `
    -AddressPrefix $SubnetBIPRange `
    -Verbose
Add-AzVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name $SubnetCName `
    -AddressPrefix $SubnetCIPRange `
    -Verbose
Set-AzVirtualNetwork `
    -VirtualNetwork $vnet `
    -Verbose

该代码成功运行,并且正在生成除一件事以外的预期内容。只有SubA具有完整的信息。对于子网B和C,仅以下配置如下:

                       {
                         "Name": "SubB",
                         "AddressPrefix": [
                           "192.168.1.0/24"
                         ]
                       }

因此,我无法像这样引用子网ID:

  -SubnetId $vnet.Subnets[1].Id `

因为该字段为空:(

代码有问题吗?还是应该引用子网ID?

我想将一些VM放置在不同的子网中,在运行上述代码后,只有SubA具有SubnetID。

                     {
                         "Delegations": [],
                         "Name": "SubA",
                         "Etag": "W/\"01baac3e-3fc3-400b-a6f1-64b08f5b45e7\"",
                         "Id": "/subscriptions/XXX/resourceGroups/Test1/providers/Microsoft.Network/virtua
                     lNetworks/Test-VM/subnets/SubA",
                         "AddressPrefix": [
                           "192.168.0.0/24"
                         ],
                         "IpConfigurations": [],
                         "ServiceAssociationLinks": [],
                         "ResourceNavigationLinks": [],
                         "ServiceEndpoints": [],
                         "ServiceEndpointPolicies": [],
                         "InterfaceEndpoints": [],
                         "ProvisioningState": "Succeeded"
                       },

1 个答案:

答案 0 :(得分:0)

我已经测试了您的代码,并且可以重现您的问题,只需在代码后使用Get-AzVirtualNetwork,即可获得其他两个子网的Id

$vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $VNetName
$vnet.Subnets[1].Id
$vnet.Subnets[2].Id

enter image description here