Skip to content

Commit 70b56ae

Browse files
committed
initial commit - 2 docs
1 parent c07c451 commit 70b56ae

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Automate VNet IP Address Management with Azure IPAM Pools
3+
description:
4+
author: mbender-ms
5+
ms.author: mbender
6+
ms.service: azure-virtual-network-manager
7+
ms.topic: concept-article
8+
ms.date: 03/10/2023
9+
ms.custom: template-concept
10+
---
11+
12+
# Automate VNet IP Address Management with Azure IPAM Pools
13+
14+
Below sample script shows how you can create a script that allows you to run bulk creation of Virtual Networks using IpamPools reference, associate existing Virtual Networks using IpamPool reference, and disassociate existing Virtual Networks using IpamPool reference. The script is written in a synchronous manner to ensure that no API calls fail such that they need to be retried.
15+
16+
## Prerequisites
17+
18+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
19+
- [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).
20+
- 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).
21+
22+
## Sign in to your Azure account and select your subscription
23+
24+
To begin your configuration, sign in to your Azure account:
25+
26+
```powershell
27+
# Sign in to your Azure account
28+
Connect-AzAccount
29+
30+
# Select your subscription
31+
Set-AzContext -Subscription $sub
32+
```
33+
34+
## Bulk VNet Creation
35+
36+
Use the following code snippet to create multiple VNets using existing IPAM pools.
37+
38+
```powershell
39+
# Create 100 VNets using ipamPool
40+
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
41+
$ipamPoolPrefixAllocation = [PSCustomObject]@{
42+
Id = "<your ipam pool reference arm id>"
43+
NumberOfIpAddresses = "8"
44+
}
45+
for ($i = 0; $i -lt 100; $i++) {
46+
$subnetName = "defaultSubnet"
47+
$vnetName = "bulk-ipam-vnet-$i"
48+
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -DefaultOutboundAccess $false
49+
$job = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -Subnet $subnet -AsJob
50+
$job | Wait-Job
51+
$actual = $job | Receive-Job
52+
}
53+
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
54+
```
55+
## Disassociate existing VNets
56+
Use the following code snippet to disassociate existing VNets using IpamPools reference.
57+
58+
```powershell
59+
60+
# bulk disassociation update
61+
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
62+
$ipamPoolPrefixAllocation = $null
63+
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
64+
$vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
65+
foreach ($subnet in $vnetList[$i].Subnets) {
66+
$subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
67+
}
68+
$job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
69+
$job | Wait-Job
70+
$actual = $job | Receive-Job
71+
}
72+
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
73+
74+
```
75+
$location = "<your resource location>"
76+
$rgname = "<your resource group>" # use RG name as "*" to fetch all VNets from all RGs within subscription
77+
$sub = "<your subscription id>"
78+
79+
# select subscription
80+
Set-AzContext -Subscription $sub
81+
82+
# Create 100 VNets using ipamPool
83+
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
84+
$ipamPoolPrefixAllocation = [PSCustomObject]@{
85+
Id = "<your ipam pool reference arm id>"
86+
NumberOfIpAddresses = "8"
87+
}
88+
for ($i = 0; $i -lt 100; $i++) {
89+
$subnetName = "defaultSubnet"
90+
$vnetName = "bulk-ipam-vnet-$i"
91+
$subnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -DefaultOutboundAccess $false
92+
$job = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -IpamPoolPrefixAllocation $ipamPoolPrefixAllocation -Subnet $subnet -AsJob
93+
$job | Wait-Job
94+
$actual = $job | Receive-Job
95+
}
96+
Write-Output "Starting creation of new VNets with IpamPool reference at: " (Get-Date).ToString("HH:mm:ss")
97+
98+
# fetch all virtual networks from a resource group
99+
$vnetList = Get-AzVirtualNetwork -ResourceGroupName $rgname
100+
101+
# bulk disassociation update
102+
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
103+
$ipamPoolPrefixAllocation = $null
104+
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
105+
$vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
106+
foreach ($subnet in $vnetList[$i].Subnets) {
107+
$subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
108+
}
109+
$job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
110+
$job | Wait-Job
111+
$actual = $job | Receive-Job
112+
}
113+
Write-Output "Starting bulk disassociation for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
114+
115+
# bulk association update
116+
Write-Output "Starting bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
117+
$ipamPoolPrefixAllocation = [PSCustomObject]@{
118+
Id = "<your ipam pool reference arm id>"
119+
NumberOfIpAddresses = "8"
120+
}
121+
for ($i = 0; $i -lt @($vnetList).Count; $i++) {
122+
$vnetList[$i].AddressSpace.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
123+
foreach ($subnet in $vnetList[$i].Subnets) {
124+
$subnet.IpamPoolPrefixAllocations = $ipamPoolPrefixAllocation
125+
}
126+
$job = Set-AzVirtualNetwork -VirtualNetwork $vnetList[$i] -AsJob
127+
$job | Wait-Job
128+
$actual = $job | Receive-Job
129+
}
130+
Write-Output "Finished bulk association for existing VNets at: " (Get-Date).ToString("HH:mm:ss")
131+
132+

0 commit comments

Comments
 (0)