Private/VirtualNetwork/CalculateSubnet.ps1
function CalculateSubnet { [CmdletBinding()] param ( [Parameter(Position = 0, Mandatory = $true)][string[]] $Vnets, [Parameter(Position = 1, Mandatory = $true)][int] $NumberHostInSubnet, [Parameter(Position = 3, Mandatory = $false)][string[]] $ExistedSubnets ) Process { $mask = switch ([int]$NumberHostInSubnet) { 8 { "29"; break } 16 { "28"; break } 32 { "27"; break } 64 { "26"; break } 128 { "25"; break } 256 { "24"; break } 512 { "23"; break } 1024 { "22"; break } } foreach ($vnet in $Vnets) { #$vnet='10.2.1.0/24' $v = IpCalc -Net $vnet $totalip = [int]$v.TotalHosts $freeip = $totalip - [int]$NumberHostInSubnet $lastip = $v.Broadcast if ($freeip -ge 0) { $adress = $vnet.Split("/")[0] $adress = $adress + "/" + $mask $checkadressinsubnets = IsIpInSubnets -ExistedSubnets $ExistedSubnets -Min $v.HostMin -Broadcast $v.Broadcast if (!$checkadressinsubnets) { return $adress } $ip = $null $v = IpCalc -Net $ExistedSubnets[0] $lastip = $v.Broadcast while ($true) { if ($ip) { $t = $ip } else { $t = $lastip } $ip = NextIp $t $adress = $ip + "/" + $mask $ipcalc = IpCalc $adress $checkadressinsubnets = IsIpInSubnets -ExistedSubnets $ExistedSubnets -Min $ipcalc.HostMin -Broadcast $ipcalc.Broadcast if (!$checkadressinsubnets) { break } } $checkvnet = IpCalc $vnet -Contains $ipcalc.Broadcast if ($checkvnet) { return $adress } } continue } return $null } } |