Important
目前,Azure Virtual Network Manager中的 IP 地址管理(IPAM)功能已在Azure Virtual Network Manager可用的所有区域中正式发布。
Azure 虚拟网络管理器中的 IPAM 池允许管理虚拟网络的 IP 地址空间。 此功能有助于避免地址空间重叠,并确保使用正确的 IP 地址范围创建 VNet。
本文提供了一个示例 PowerShell 脚本,该脚本演示如何创建多个 VNet、将现有 VNet 与 IPAM 池关联,以及取消关联 VNet 与 IPAM 池。
先决条件
- 拥有有效订阅的 Azure 帐户。 创建账户。
- Azure PowerShell 本地安装的。
- 创建 IPAM 池的虚拟网络管理器实例。 有关详细信息,请参阅 创建虚拟网络管理器 并 创建 IPAM 池。
- 要在其中创建 VNet 的现有资源组。 建议使用与虚拟网络管理器实例相同的资源组,以便更好地组织和管理。
查看示例脚本
该脚本位于 GitHub 上的 Azure 示例存储库中。 可以从以下链接查看和下载脚本: automate-vnet-ip-address-management.ps1
示例脚本
# 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")
登录到 Azure 帐户,然后选择订阅
如果在本地使用 Azure PowerShell,请登录到 Azure 帐户:
# Sign in to your Azure account
Connect-AzAccount -Environment AzureChinaCloud
# Select your subscription
Set-AzContext -Subscription <subscriptionId>
下载脚本
将脚本下载到本地目录或首选 PowerShell 环境。 可以使用以下命令直接从 Azure 示例存储库下载脚本:
# 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"
更新脚本变量
下载脚本后,在首选 PowerShell 编辑器中打开该脚本,并更新以下变量以匹配环境:
| 变量 | 说明 |
|---|---|
$location |
输入要在其中创建 VNet(如 美国东部)的 Azure 区域。 |
$rgname |
输入要在其中创建 VNet 的资源组的名称。 可以使用 "*" 提取订阅中所有资源组的所有 VNet。 |
$sub |
输入要在其中创建 VNet 的订阅 ID。 可以使用 "*" 提取租户中所有订阅的所有 VNet。 |
$ipamPoolARMId |
要用于 VNet 的 IPAM 池的 Azure 资源管理器 ID,类似于 "/subscriptions/<your subscription id>/resourceGroups/<your resource group>/providers/Microsoft.Network/ipamPools/<your ipam pool name>"。 |
$numberIPaddresses |
要从 IPAM 池分配的 IP 地址数。 这应该是基于 IPAM 池配置的有效数字。 |
对于 Visual Studio Code 或其他 PowerShell 编辑器,请输入以下代码以在编辑器中打开脚本:
# Open the script in Visual Studio Code
code ./automate-vnet-ip-address-management.ps1
在运行脚本之前,请记得保存脚本。
运行脚本
更新脚本变量后,可以在 PowerShell 环境中运行该脚本。 该脚本使用 IPAM 池引用创建 10 个 VNet,从 IPAM 池中取消关联现有 VNet,然后将其与 IPAM 池重新关联。
# Run the script
./automate-vnet-ip-address-management.ps1
示例输出
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
HasMoreData : True
Location : localhost
StatusMessage : Completed
CurrentPSTransaction :
Host : System.Management.Automation.Internal.Host.InternalHost
Command : New-AzVirtualNetwork
JobStateInfo : Completed
Finished : System.Threading.ManualResetEvent
InstanceId : b05bce55-99b6-4a91-b1b7-cf6da245def1
Id : 3
Name : Long Running Operation for 'New-AzVirtualNetwork' on resource 'bulk-ipam-vnet-0'
ChildJobs : {}
PSBeginTime : 3/12/2025 6:49:06 PM
PSEndTime : 3/12/2025 6:49:22 PM
PSJobTypeName : AzureLongRunningJob`1
Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork}
Error : {}
Progress : {}
Verbose : {}
Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for cmdlet confirmation required: 'False', [AzureLongRunningJob]: Completing cmdlet execution in RunJob}
Warning : {}
Information : {}
State : Completed
Starting creation of new VNets with IpamPool reference at:
18:49:37
Starting bulk disassociation for existing VNets at:
18:49:37
HasMoreData : True
Location : localhost
StatusMessage : Completed
CurrentPSTransaction :
Host : System.Management.Automation.Internal.Host.InternalHost
Command : Set-AzVirtualNetwork
JobStateInfo : Completed
Finished : System.Threading.ManualResetEvent
InstanceId : cccccccc-2222-3333-4444-dddddddddddd
Id : 5
Name : Long Running Operation for 'Set-AzVirtualNetwork'
ChildJobs : {}
PSBeginTime : 3/12/2025 6:49:37 PM
PSEndTime : 3/12/2025 6:49:48 PM
PSJobTypeName : AzureLongRunningJob`1
Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork}
Error : {}
Progress : {}
Verbose : {}
Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for cmdlet confirmation required: 'False', [AzureLongRunningJob]: Completing cmdlet execution in RunJob}
Warning : {}
Information : {}
State : Completed
Starting bulk disassociation for existing VNets at:
18:49:59
Starting bulk association for existing VNets at:
18:49:59
HasMoreData : True
Location : localhost
StatusMessage : Completed
CurrentPSTransaction :
Host : System.Management.Automation.Internal.Host.InternalHost
Command : Set-AzVirtualNetwork
JobStateInfo : Completed
Finished : System.Threading.ManualResetEvent
InstanceId : bbbbbbbb-1111-2222-3333-cccccccccccc
Id : 7
Name : Long Running Operation for 'Set-AzVirtualNetwork'
ChildJobs : {}
PSBeginTime : 3/12/2025 6:49:59 PM
PSEndTime : 3/12/2025 6:50:16 PM
PSJobTypeName : AzureLongRunningJob`1
Output : {Microsoft.Azure.Commands.Network.Models.PSVirtualNetwork}
Error : {}
Progress : {}
Verbose : {}
Debug : {[AzureLongRunningJob]: Starting cmdlet execution, setting for
cmdlet confirmation required: 'False', [AzureLongRunningJob]:
Completing cmdlet execution in RunJob}
Warning : {}
Information : {}
State : Completed
Finished bulk association for existing VNets at:
18:50:32
PS /home/michael/clouddrive/avnm-script>
注释
该脚本以同步方式运行,以确保没有 API 调用失败。 因此,脚本可能需要一些时间才能完成,具体取决于要创建的和管理 VNet 的数量。
验证虚拟网络
若要验证 VNet 是否已创建并与 IPAM 池关联,可以使用以下命令:
# List all VNets in the specified resource group
Get-AzVirtualNetwork -ResourceGroupName $rgname | Select-Object Name, Location, AddressSpace, IpamPoolPrefixAllocations
此命令显示指定资源组中每个虚拟网络的名称、位置、地址空间和 IPAM 池前缀分配。 应会看到使用 IPAM 池引用创建的 VNet。