|
| 1 | +--- |
| 2 | +title: Automate virtual network IP Address Management with Azure IPAM Pools |
| 3 | +description: This article provides a sample PowerShell script to automate the creation and management of VNets using IPAM pools in Azure Virtual Network Manager. |
| 4 | +author: mbender-ms |
| 5 | +ms.author: mbender |
| 6 | +ms.service: azure-virtual-network-manager |
| 7 | +ms.topic: sample |
| 8 | +ms.date: 03/14/2025 |
| 9 | +ms.custom: template-concept |
| 10 | +--- |
| 11 | + |
| 12 | +# Automate virtual network IP Address Management with Azure IPAM Pools |
| 13 | + |
| 14 | +IPAM Pools in Azure Virtual Network Manager allow you to 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. |
| 15 | + |
| 16 | +In this article, we provide a sample PowerShell script that demonstrates how to create multiple VNets, associate existing VNets with IPAM pools, and disassociate VNets from IPAM pools. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- [Azure PowerShell](https://docs.microsoft.com/powershell/azure/new-azureps-module-az?view=azps-7.4.0) installed locally or use [Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/overview). |
| 22 | +- A virtual network manager instance with an IPAM pool created. For more information, see [Create a virtual network manager](./create-virtual-network-manager-powershell.md) and [Create an IPAM pool](./how-to-manage-ip-addresses-network-manager.md). |
| 23 | +- 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. |
| 24 | + |
| 25 | + |
| 26 | +## Review the sample script |
| 27 | + |
| 28 | +The script is located in the Azure Samples repository on GitHub. You can view and download the script from the following link: |
| 29 | +[automate-vnet-ip-address-management.ps1](https://github.com/Azure-Samples/azure-docs-powershell-samples/blob/main/virtual-network-manager/automate-vnet-ip-address-management.ps1) |
| 30 | + |
| 31 | +### Sample script |
| 32 | + |
| 33 | +[!Code-powershell[main](../../azure_powershell_scripts/virtual-network-manager/automate-vnet-ip-address-management.ps1?range=19-80)] |
| 34 | + |
| 35 | +## Sign in to your Azure account and select your subscription |
| 36 | + |
| 37 | +If you're using Azure PowerShell locally, sign in to your Azure account: |
| 38 | + |
| 39 | +```powershell |
| 40 | +# Sign in to your Azure account |
| 41 | +Connect-AzAccount |
| 42 | +
|
| 43 | +# Select your subscription |
| 44 | +Set-AzContext -Subscription <subscriptionId> |
| 45 | +``` |
| 46 | + |
| 47 | +Or sign in to [Azure Cloud Shell](https://shell.azure.com) and select your subscription: |
| 48 | + |
| 49 | +```powershell |
| 50 | +# Select your subscription |
| 51 | +Set-AzContext -Subscription <subscriptionId> |
| 52 | +``` |
| 53 | +## Download the script |
| 54 | + |
| 55 | +Download the script to a local directory or your preferred PowerShell environment include [Azure Cloud Shell](https://shell.azure.com). You can use the following command to download the script directly from the Azure Samples repository: |
| 56 | + |
| 57 | +```powershell |
| 58 | +# Download the script |
| 59 | +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" |
| 60 | +
|
| 61 | +``` |
| 62 | + |
| 63 | +## Update the script variables |
| 64 | + |
| 65 | +After you download the script, open it in your preferred PowerShell editor and update the following variables to match your environment: |
| 66 | + |
| 67 | +| **Variable** | **Description** | |
| 68 | +|----------|-------------| |
| 69 | +| `$location` | Enter the Azure region where you want to create the VNets such as *East US*. | |
| 70 | +| `$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. | |
| 71 | +| `$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. | |
| 72 | +| `$ipamPoolARMId` | The Azure Resource Manager ID of the IPAM pool you want to use for the VNets similar to `"/subscriptions/<your subscription id>/resourceGroups/<your resource group>/providers/Microsoft.Network/ipamPools/<your ipam pool name>"`. | |
| 73 | +| `$numberIPaddresses` | The number of IP addresses to allocate from the IPAM pool. This should be a valid number based on your IPAM pool configuration. | |
| 74 | + |
| 75 | +For Visual Studio Code or another PowerShell editor, enter the following code to open the script in your editor: |
| 76 | + |
| 77 | +```powershell |
| 78 | +# Open the script in Azure Cloud Shell editor or Visual Studio Code |
| 79 | +code ./automate-vnet-ip-address-management.ps1 |
| 80 | +``` |
| 81 | + |
| 82 | +For Azure Cloud Shell, enter the following code to open the script in your editor: |
| 83 | + |
| 84 | +```powershell |
| 85 | +# Open the script in Azure Cloud Shell editor |
| 86 | +code automate-vnet-ip-address-management.ps1 |
| 87 | +``` |
| 88 | + |
| 89 | +Remember to save your script before running it. |
| 90 | + |
| 91 | +## Run the Script |
| 92 | + |
| 93 | +After updating the script variables, you can run the script in your PowerShell environment. The script creates 10 VNets using the IPAM pool reference, disassociate existing VNets from the IPAM pool, and then re-associate them with the IPAM pool. |
| 94 | + |
| 95 | +```powershell |
| 96 | +# Run the script |
| 97 | +./automate-vnet-ip-address-management.ps1 |
| 98 | +``` |
| 99 | + |
| 100 | +### Sample output |
| 101 | + |
| 102 | +```powershell |
| 103 | +
|
| 104 | +PS /home/michael/clouddrive/avnm-script> ./automate-vnet-ip-address-management.ps1 |
| 105 | +
|
| 106 | + Tenant: aaaabbbb-0000-cccc-1111-dddd2222eeee |
| 107 | +
|
| 108 | +SubscriptionName SubscriptionId Account Environment |
| 109 | +---------------- -------------- ------- ----------- |
| 110 | +Azure Subscription aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e user@azure AzureCloud |
| 111 | +Starting creation of new VNets with IpamPool reference at: |
| 112 | +18:49:06 |
| 113 | +
|
| 114 | +HasMoreData : True |
| 115 | +Location : localhost |
| 116 | +StatusMessage : Completed |
| 117 | +CurrentPSTransaction : |
| 118 | +Host : System.Management.Automation.Internal.Host.InternalHost |
| 119 | +Command : New-AzVirtualNetwork |
| 120 | +JobStateInfo : Completed |
| 121 | +Finished : System.Threading.ManualResetEvent |
| 122 | +InstanceId : b05bce55-99b6-4a91-b1b7-cf6da245def1 |
| 123 | +Id : 3 |
| 124 | +Name : Long Running Operation for 'New-AzVirtualNetwork' on resource 'bulk-ipam-vnet-0' |
| 125 | +ChildJobs : {} |
| 126 | +PSBeginTime : 3/12/2025 6:49:06 PM |
| 127 | +PSEndTime : 3/12/2025 6:49:22 PM |
| 128 | +PSJobTypeName : AzureLongRunningJob`1 |
| 129 | +Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork} |
| 130 | +Error : {} |
| 131 | +Progress : {} |
| 132 | +Verbose : {} |
| 133 | +Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for cmdlet confirmation required: 'False', [AzureLongRunningJob]: Completing cmdlet execution in RunJob} |
| 134 | +Warning : {} |
| 135 | +Information : {} |
| 136 | +State : Completed |
| 137 | +
|
| 138 | +Starting creation of new VNets with IpamPool reference at: |
| 139 | +18:49:37 |
| 140 | +Starting bulk disassociation for existing VNets at: |
| 141 | +18:49:37 |
| 142 | +
|
| 143 | +HasMoreData : True |
| 144 | +Location : localhost |
| 145 | +StatusMessage : Completed |
| 146 | +CurrentPSTransaction : |
| 147 | +Host : System.Management.Automation.Internal.Host.InternalHost |
| 148 | +Command : Set-AzVirtualNetwork |
| 149 | +JobStateInfo : Completed |
| 150 | +Finished : System.Threading.ManualResetEvent |
| 151 | +InstanceId : cccccccc-2222-3333-4444-dddddddddddd |
| 152 | +Id : 5 |
| 153 | +Name : Long Running Operation for 'Set-AzVirtualNetwork' |
| 154 | +ChildJobs : {} |
| 155 | +PSBeginTime : 3/12/2025 6:49:37 PM |
| 156 | +PSEndTime : 3/12/2025 6:49:48 PM |
| 157 | +PSJobTypeName : AzureLongRunningJob`1 |
| 158 | +Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork} |
| 159 | +Error : {} |
| 160 | +Progress : {} |
| 161 | +Verbose : {} |
| 162 | +Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for cmdlet confirmation required: 'False', [AzureLongRunningJob]: Completing cmdlet execution in RunJob} |
| 163 | +Warning : {} |
| 164 | +Information : {} |
| 165 | +State : Completed |
| 166 | +
|
| 167 | +Starting bulk disassociation for existing VNets at: |
| 168 | +18:49:59 |
| 169 | +Starting bulk association for existing VNets at: |
| 170 | +18:49:59 |
| 171 | +
|
| 172 | +HasMoreData : True |
| 173 | +Location : localhost |
| 174 | +StatusMessage : Completed |
| 175 | +CurrentPSTransaction : |
| 176 | +Host : System.Management.Automation.Internal.Host.InternalHost |
| 177 | +Command : Set-AzVirtualNetwork |
| 178 | +JobStateInfo : Completed |
| 179 | +Finished : System.Threading.ManualResetEvent |
| 180 | +InstanceId : bbbbbbbb-1111-2222-3333-cccccccccccc |
| 181 | +Id : 7 |
| 182 | +Name : Long Running Operation for 'Set-AzVirtualNetwork' |
| 183 | +ChildJobs : {} |
| 184 | +PSBeginTime : 3/12/2025 6:49:59 PM |
| 185 | +PSEndTime : 3/12/2025 6:50:16 PM |
| 186 | +PSJobTypeName : AzureLongRunningJob`1 |
| 187 | +Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork} |
| 188 | +Error : {} |
| 189 | +Progress : {} |
| 190 | +Verbose : {} |
| 191 | +Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for |
| 192 | + cmdlet confirmation required: 'False', [AzureLongRunningJob]: |
| 193 | + Completing cmdlet execution in RunJob} |
| 194 | +Warning : {} |
| 195 | +Information : {} |
| 196 | +State : Completed |
| 197 | +
|
| 198 | +Finished bulk association for existing VNets at: |
| 199 | +18:50:32 |
| 200 | +
|
| 201 | +PS /home/michael/clouddrive/avnm-script> |
| 202 | +``` |
| 203 | + |
| 204 | +> [!NOTE] |
| 205 | +> 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. |
| 206 | +
|
| 207 | +## Verify the virtual networks |
| 208 | + |
| 209 | +To verify that the VNets were created and associated with the IPAM pool, you can use the following command: |
| 210 | + |
| 211 | +```powershell |
| 212 | +# List all VNets in the specified resource group |
| 213 | +Get-AzVirtualNetwork -ResourceGroupName $rgname | Select-Object Name, Location, AddressSpace, IpamPoolPrefixAllocations |
| 214 | +``` |
| 215 | + |
| 216 | +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. |
| 217 | + |
| 218 | +## Next steps |
| 219 | + |
| 220 | +> [!div class="nextstepaction"] |
| 221 | +> [Prevent overlapping IP Address space with Azure Policy and IPAM pools](./prevent-overlapping-ip-address-space-policy-ipam.md) |
0 commit comments