0
votes

Context

I have an existing RM virtual network, and now I would like to add a new subnet using PowerShell:

# Create subnet config:
$besub = New-AzureRmVirtualNetworkSubnetConfig -Name $BESubName -AddressPrefix $BESubPrefix

# Get VNet
$vnet  = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vmResourceGroup

# Add subnet:
Add-AzureRmVirtualNetworkSubnetConfig -Name $BESubName -AddressPrefix $BESubPrefix -VirtualNetwork $vnet 

All commands run successfully however no subnet added to my virtual network.

What I've try so far:

  • Originally I've thought there should be a Update-AzureRmVirtualNetwork, but there is no such a command.
  • I've also searched for New-AzureRmVirtualNetworkSubnet, which has a -VirtualNetwork $vnet parameter with no success.

Question

How can I add new RM Subnet to my existing RM Virtual Network?

1
for my understanding, those changes are only saved on your local computer, you will need a Set-AzureRmVirtualNetwork -VirtualNetwork $vnet to save them to azureKai Zhao
@Kai, many thanks that would be an answer...g.pickardou

1 Answers

3
votes

You are close. You're creating the config but it is not getting applied. This script below will do it for you.

# Get VNet
$vnet  = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $vmResourceGroup

# Add subnet config to vnet:
Add-AzureRmVirtualNetworkSubnetConfig -Name $BESubName -AddressPrefix $BESubPrefix -VirtualNetwork $vnet 

# Apply subnet config to vnet:
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet