Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Currently, the IP address management (IPAM) feature in Azure Virtual Network Manager is generally available in all regions where Azure Virtual Network Manager is available.
IPAM pools in Azure Virtual Network Manager help you manage IP address spaces for your virtual networks. This feature helps you avoid overlapping address spaces and ensures that your VNets are created with the correct IP address ranges.
This article provides a sample PowerShell script that demonstrates how to create multiple VNets, associate existing VNets with IPAM pools, and disassociate VNets from IPAM pools.
Prerequisites
- An Azure account with an active subscription. Create an account.
- Azure PowerShell installed locally.
- A virtual network manager instance with an IPAM pool created. For more information, see Create a virtual network manager and Create an IPAM pool.
- An existing resource group where you want to create the VNets. It's recommended to use the same resource group as the virtual network manager instance for better organization and management.
Review the sample script
The script is located in the Azure Samples repository on GitHub. You can view and download the script from the following link: automate-vnet-ip-address-management.ps1
After you set the location, resource group, subscription, IPAM pool resource ID, and number of IP addresses at the top of the script, it performs three operations in order:
- Bulk create virtual networks from the pool. For each of 10 virtual networks, the script builds a subnet configuration with New-AzVirtualNetworkSubnetConfig and creates the virtual network with New-AzVirtualNetwork, passing the IPAM pool reference to the
-IpamPoolPrefixAllocationparameter on both. The pool allocates the address space. - Bulk disassociate existing virtual networks from the pool. The script retrieves the virtual networks in the resource group with Get-AzVirtualNetwork, clears
IpamPoolPrefixAllocationson each virtual network's address space and on each of its subnets, and saves the change with Set-AzVirtualNetwork. - Bulk reassociate those virtual networks with the pool. The script restores the IPAM pool reference on the same address spaces and subnets and saves the change with
Set-AzVirtualNetwork.
Each operation runs its updates as jobs and waits for each job to finish before starting the next, so the API calls complete in order rather than needing to be retried.
Sample script
# Set the variables for the script to your environment
$location = "<your resource location>" # e.g. "East US", "West Europe", etc.
$rgname = "<your resource group>" # use RG name as "*" to fetch all VNets from all RGs within subscription
$sub = "<your subscription id>" # use subscription id as "*" to fetch all VNets from all subscriptions within tenant
$ipamPoolARMId = "<your ipam pool ARM ID>" # e.g. "/subscriptions/<your subscription id>/resourceGroups/<your resource group>/providers/Microsoft.Network/ipamPools/<your ipam pool name>"
$numberIPaddresses = "8" # Number of IP addresses to allocate from the IPAM Pool. This should be a valid number based on your IPAM Pool configuration.
# Select your subscription
Set-AzContext -Subscription $sub
# Set the
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = [PSCustomObject]@{
Id = $ipamPoolARMId
NumberOfIpAddresses = $numberIPaddresses
}
# Create 10 VNets using ipamPool reference - Change the number of VNets to create as needed in the for loop below
for ($i = 0; $i -lt 10; $i++) {
$subnetName = "defaultSubnet"
$vnetName = "bulk-ipam-vnet-$i"
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -DefaultOutboundAccess $false
$job = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -Subnet $subnet -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
}
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
# fetch all virtual networks from a resource group
$vnetList = Get-AzVirtualNetwork -ResourceGroupName $rgname
# bulk disassociation update
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = $null
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
$vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
foreach ($subnet in $vnetList[$i].Subnets) {
$subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
}
$job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
}
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
# bulk association update
Write-Output "Starting bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
$ipamPoolPrefixAllocation = [PSCustomObject]@{
Id = $ipamPoolARMId
NumberOfIpAddresses = $numberIPaddresses
}
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
$vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
foreach ($subnet in $vnetList[$i].Subnets) {
$subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
}
$job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
$job | Wait-Job
$actual = $job | Receive-Job
}
Write-Output "Finished bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
Sign in to your Azure account and select your subscription
If you're using Azure PowerShell locally, sign in to your Azure account:
# Sign in to your Azure account
Connect-AzAccount -Environment AzureChinaCloud
# Select your subscription
Set-AzContext -Subscription <subscriptionId>
Download the script
Download the script to a local directory or your preferred PowerShell environment. You can use the following command to download the script directly from the Azure Samples repository:
# Download the script
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/Azure-Samples/azure-docs-powershell-samples/main/virtual-network-manager/automate-vnet-ip-address-management.ps1" -OutFile "automate-vnet-ip-address-management.ps1"
Update the script variables
After you download the script, open it in your preferred PowerShell editor and update the following variables to match your environment:
| Variable | Description |
|---|---|
$location |
Enter the Azure region where you want to create the VNets such as East US. |
$rgname |
Enter the name of the resource group where you want to create the VNets. You can use "*" to fetch all VNets from all resource groups within the subscription. |
$sub |
Enter the subscription ID where you want to create the VNets. You can use "*" to fetch all VNets from all subscriptions within the tenant. |
$ipamPoolARMId |
The Azure Resource Manager ID of the IPAM pool you want to use for the VNets, similar to "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Network/networkManagers/<networkManagerName>/ipamPools/<ipAddressPoolName>". You can copy this resource ID from the pool's JSON view in the Azure portal. |
$numberIPaddresses |
The number of IP addresses to allocate from the IPAM pool. This should be a valid number based on your IPAM pool configuration. |
For Visual Studio Code, enter the following command to open the script in your editor:
# Open the script in Visual Studio Code
code ./automate-vnet-ip-address-management.ps1
Remember to save your script before running it.
Run the script
After updating the script variables, run the script in your PowerShell environment. The script creates 10 VNets using the IPAM pool reference, disassociates existing VNets from the IPAM pool, and then reassociates them with the IPAM pool.
# Run the script
./automate-vnet-ip-address-management.ps1
Sample output
PS /home/michael/clouddrive/avnm-script> ./automate-vnet-ip-address-management.ps1
Tenant: aaaabbbb-0000-cccc-1111-dddd2222eeee
SubscriptionName SubscriptionId Account Environment
---------------- -------------- ------- -----------
Azure Subscription aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e user@azure AzureCloud
Starting creation of new VNets with IpamPool reference at:
18:49:06
Starting creation of new VNets with IpamPool reference at:
18:49:37
Starting bulk disassociation for existing VNets at:
18:49:37
Starting bulk disassociation for existing VNets at:
18:49:59
Starting bulk association for existing VNets at:
18:49:59
Finished bulk association for existing VNets at:
18:50:32
PS /home/michael/clouddrive/avnm-script>
Note
The script runs synchronously to ensure that no API calls fail. Because of this, the script can take some time to complete, depending on the number of VNets being created and managed.
Verify the virtual networks
To verify that the VNets were created and associated with the IPAM pool, you can use the following command:
# List all VNets in the specified resource group
Get-AzVirtualNetwork -ResourceGroupName $rgname | Select-Object Name, Location, AddressSpace, @{Name = "IpamPoolPrefixAllocations"; Expression = { $_.AddressSpace.IpamPoolPrefixAllocations }}
This command displays the name, location, address space, and IPAM pool prefix allocations for each virtual network in the specified resource group. You should see the VNets you created with the IPAM pool reference.