Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Med IPAM-pooler i Azure Virtual Network Manager kan du hantera IP-adressutrymmen för dina virtuella nätverk. Den här funktionen hjälper dig att undvika överlappande adressutrymmen och ser till att dina virtuella nätverk skapas med rätt IP-adressintervall.
I den här artikeln tillhandahåller vi ett PowerShell-exempelskript som visar hur du skapar flera virtuella nätverk, associerar befintliga virtuella nätverk med IPAM-pooler och kopplar bort virtuella nätverk från IPAM-pooler.
Förutsättningar
- Ett Azure-konto med en aktiv prenumeration. Skapa ett konto utan kostnad.
- Azure PowerShell installeras lokalt eller använder Azure Cloud Shell.
- En virtuell nätverkshanterareinstans med en IPAM-pool skapad. Mer information finns i Skapa en virtuell nätverkshanterare och Skapa en IPAM-pool.
- En befintlig resursgrupp där du vill skapa de virtuella nätverken. Vi rekommenderar att du använder samma resursgrupp som den virtuella nätverkshanterarens instans för bättre organisation och hantering.
Granska exempelskriptet
Skriptet finns i Azure Samples-lagringsplatsen på GitHub. Du kan visa och ladda ned skriptet från följande länk: automate-vnet-ip-address-management.ps1
Exempelskript
# 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")
Logga in på ditt Azure-konto och välj din prenumeration
Om du använder Azure PowerShell lokalt loggar du in på ditt Azure-konto:
# Sign in to your Azure account
Connect-AzAccount
# Select your subscription
Set-AzContext -Subscription <subscriptionId>
Eller logga in på Azure Cloud Shell och välj din prenumeration:
# Select your subscription
Set-AzContext -Subscription <subscriptionId>
Ladda ned skriptet
Ladda ned skriptet till en lokal katalog eller din önskade PowerShell-miljö, inklusive Azure Cloud Shell. Du kan använda följande kommando för att ladda ned skriptet direkt från Azure Samples-lagringsplatsen:
# 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"
Uppdatera skriptvariablerna
När du har laddat ned skriptet öppnar du det i önskad PowerShell-redigerare och uppdaterar följande variabler så att de matchar din miljö:
variabel | Beskrivning |
---|---|
$location |
Ange den Azure-region där du vill skapa de virtuella nätverken, till exempel USA, östra. |
$rgname |
Ange namnet på resursgruppen där du vill skapa de virtuella nätverken. Du kan använda "*" för att hämta alla VNets från alla resursgrupper inom prenumerationen. |
$sub |
Ange prenumerations-ID:t där du vill skapa virtuella nätverk. Du kan använda "*" för att hämta alla VNets från alla prenumerationer i klientorganisationen. |
$ipamPoolARMId |
Azure Resource Manager-ID för IPAM-poolen du vill använda för virtuella nätverk liknande "/subscriptions/<your subscription id>/resourceGroups/<your resource group>/providers/Microsoft.Network/ipamPools/<your ipam pool name>" . |
$numberIPaddresses |
Antalet IP-adresser som ska allokeras från IPAM-poolen. Det här bör vara ett giltigt nummer baserat på konfigurationen av IPAM-poolen. |
För Visual Studio Code eller någon annan PowerShell-redigerare anger du följande kod för att öppna skriptet i redigeringsprogrammet:
# Open the script in Azure Cloud Shell editor or Visual Studio Code
code ./automate-vnet-ip-address-management.ps1
För Azure Cloud Shell anger du följande kod för att öppna skriptet i redigeringsprogrammet:
# Open the script in Azure Cloud Shell editor
code automate-vnet-ip-address-management.ps1
Kom ihåg att spara skriptet innan du kör det.
Kör skriptet
När du har uppdaterat skriptvariablerna kan du köra skriptet i PowerShell-miljön. Skriptet skapar 10 virtuella nätverk med ipam-poolreferensen, kopplar bort befintliga virtuella nätverk från IPAM-poolen och associerar dem sedan igen med IPAM-poolen.
# Run the script
./automate-vnet-ip-address-management.ps1
Exempelresultat
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>
Anmärkning
Skriptet körs synkront för att säkerställa att inga API-anrop misslyckas. På grund av detta kan skriptet ta lite tid att slutföra, beroende på antalet virtuella nätverk som skapas och hanteras.
Verifiera de virtuella nätverken
Om du vill kontrollera att de virtuella nätverken har skapats och associerats med IPAM-poolen kan du använda följande kommando:
# List all VNets in the specified resource group
Get-AzVirtualNetwork -ResourceGroupName $rgname | Select-Object Name, Location, AddressSpace, IpamPoolPrefixAllocations
Det här kommandot visar namn, plats, adressutrymme och IPAM-poolprefixallokeringar för varje virtuellt nätverk i den angivna resursgruppen. Du bör se de virtuella nätverk som du skapade med IPAM-poolreferensen.