Jul 29 2023 11:42 PM
While executing the powershell script as per below script which i made for check avaiable IP and free CIDR in exacting Vnet in Azure but getting failed as per below error.
# Connect to your Azure account (if not already connected) Connect-AzAccount -Subscription xyz-sub # Define the Azure resource group and virtual network name $resourceGroupName = "network-01-rg" $virtualNetworkName = "network-01-vnet" # Get the virtual network object $virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName # Check if the virtual network exists if (!$virtualNetwork) { Write-Host "Virtual network '$virtualNetworkName' not found in resource group '$resourceGroupName'." return } # Function to find a free IP CIDR range in a given subnet function Get-FreeIPCIDRRange { param ( [string]$subnetAddressPrefix, [array]$existingSubnets ) $subnetCIDR = [System.Net.IPAddress]::Parse($subnetAddressPrefix).GetAddressBytes() $subnetBits = $subnetAddressPrefix.Split("/")[1] foreach ($existingSubnet in $existingSubnets) { $existingCIDR = [System.Net.IPAddress]::Parse($existingSubnet.AddressPrefix).GetAddressBytes() $existingBits = $existingSubnet.AddressPrefix.Split("/")[0] # Calculate the number of addresses in each subnet $totalAddresses = [System.Math]::Pow(2, 32 - $existingBits) # Calculate the first address of the existing subnet $existingStartAddress = $existingCIDR[0] -band ([System.Net.IPAddress]::Broadcast -bxor ([System.Math]::Pow(2, 32) - [System.Math]::Pow(2, 32 - $existingBits))) # Calculate the last address of the existing subnet $existingEndAddress = $existingStartAddress + $totalAddresses - 1 # Calculate the first address of the new subnet $newStartAddress = $subnetCIDR[0] -band ([System.Net.IPAddress]::Broadcast -bxor ([System.Math]::Pow(2, 32) - [System.Math]::Pow(2, 32 - $subnetBits))) # If the new subnet's first address is greater than the existing subnet's last address, the range is free if ($newStartAddress -gt $existingEndAddress) { return $subnetAddressPrefix } } return $null } # Retrieve the existing subnets in the virtual network $existingSubnets = $virtualNetwork.Subnets # Find a free IP CIDR range for the new subnet $availableCIDR = $null for ($i = 32; $i -le 29; $i++) { $proposedCIDR = "172.28.$i.0/$i" $availableCIDR = Get-FreeIPCIDRRange -subnetAddressPrefix $proposedCIDR -existingSubnets $existingSubnets if ($availableCIDR -ne $null) { break } } # Check if a free IP CIDR range was found if ($availableCIDR -ne $null) { Write-Host "A free IP CIDR range is available: $availableCIDR" } else { Write-Host "No free IP CIDR range is available in the virtual network '$virtualNetworkName'." }
Output Screenshot:-
What should i do changes in script so can get as an output with new free IP with CIDR range.
Jul 30 2023 09:03 AM - edited Jul 30 2023 09:06 AM
my above question/script actually i want to know the free avaibale IP address with CIDR in exacting Vnet? (let say a new requiement are come in my project for create new subnet in exacting Vnet with 172.28.x.x/25 so these thing now we are doing with help of the attacged url link) and now i want to do automatically with help of script davidc.net/sites/default/subnets/subnets.html (using this url for deviding with one ip/cidr subnet to multiple ip/cider subnet –
Attached SS for reference.