为什么使用 Azure 资源管理器 Powershell cmdlet 创建的子网的 ID 为空?

Why is the ID for a subnet created using Azure Resource Manager Powershell cmdlets null?

我正在尝试在 Azure 中配置一些网络资源,我 运行 进入了一个阻止程序,其中一个步骤试图引用先前创建的子网 Id 属性.

这是检索子网详细信息的命令:

$gwsubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet

然后在控制台输出$gwsubnet的值:

Name : GatewaySubnet Id : Etag : ProvisioningState : AddressPrefix : 10.1.1.0/24 IpConfigurations : null NetworkSecurityGroup : null RouteTable : null

请注意 Id 属性 为空。这会导致后续步骤出错,例如:

$gwipconfig = New-AzureRmVirtualNetworkGatewayIpConfig -Name config1 -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id

给出以下错误:

New-AzureRmVirtualNetworkGatewayIpConfig : Cannot validate argument on parameter 'SubnetId'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At line:1 char:99 + ... nfig -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [New-AzureRmVirtualNetworkGatewayIpConfig], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Network.NewAzureVirtualNetworkGatewayIpConfigCommand

我正在为如何为我的子网分配 Id 而苦恼。这是我需要再次 logout/login 才能看到的内容吗?感谢您的帮助。

您遇到的问题是在创建虚拟网络之前不会创建子网 ID。

当你 运行 Get-AzureRmVirtualNetworkSubnetConfig 你创建的只是一个配置附加到 New-AzureRmVirtualNetwork 一旦你有 运行 该命令它将连接到 Azure,部署 Vnet ,创建子网,并在该状态下创建一个子网 ID。

下面的代码会告诉你我的意思...

$DNSNameLabel = "mydnsname"
$NetworkName = "MyNet"
$NICName = "MyNIC"
$PublicIPAddressName = "MyPIP"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"

$SingleSubnet = New-AzureRmVirtualNetworkSubnetConfig `
                -Name $SubnetName `
                -AddressPrefix $SubnetAddressPrefix

#At this point nothing is created, so there is no valid subnet id

$Vnet = New-AzureRmVirtualNetwork -Name $NetworkName `
                -ResourceGroupName $ResourceGroupName `
                -Location $LocationName `
                -AddressPrefix $VnetAddressPrefix `
                -Subnet $SingleSubnet

#vnet and subnet are created, both get an id and etag

 $vnet = Get-AzureRmVirtualNetwork -Name $NetworkName 
                -ResourceGroupName $ResourceGroupName 

$vnet.Subnets[0].Id 

哪个会给出

/subscriptions/{Subscription ID}/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyNet/subnets/MySubnet