Functions/Networking/Get-IPRange.ps1
<#
.SYNOPSIS Get the IP addresses in a range based on a given ip and mask/cidr .EXAMPLE Get-IPrangeStartEnd -ip 192.168.8.3 -cidr 24 #> function Get-IPRange { param ( [Parameter(Mandatory=$true)] [string] $IP, [Parameter(ParameterSetName='MASK')] [string] $Mask, [Parameter(ParameterSetName='CIDR')] [int] $CIDR ) Process { # Parse IP and Subnet Mask $ipaddr = [Net.IPAddress]::Parse($ip) $MaskAddr = if($mask){[Net.IPAddress]::Parse($mask)} elseif($CIDR){[Net.IPAddress]::Parse((Convert-CIDRtoSubnetMask -CIDR $CIDR))} # Define Network and Broadcast Address $networkaddr = new-object net.ipaddress ($maskaddr.address -band $ipaddr.address) $broadcastaddr = new-object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $maskaddr.address -bor $networkaddr.address)) # Define Beginning and Ending Address in INT64 $startaddr = Convert-IPtoInt64 -IPAddress $networkaddr.IPAddressToString $endaddr = Convert-IPtoInt64 -IPAddress $broadcastaddr.IPAddressToString # Enumerate Range of IPs in this Int64 range $IPRange = for ($i = $startaddr; $i -le $endaddr; $i++) { Convert-Int64toIP -INT64 $i } } End { # Output IP Range $IPRange } } |