#=========================================================================================== # AUTHOR: Tao Yang # DATE: 09/02/2012 # Version: 1.0 # COMMENT: Calculate first and last IP based on any given IP and subnet mask #=========================================================================================== Param ([string]$IP, [string]$SubnetMask) #Start Functions function Validate-IP ($strIP) { $bValidIP = $true $arrSections = @() $arrSections +=$strIP.split(".") #firstly, make sure there are 4 sections in the IP address if ($arrSections.count -ne 4) {$bValidIP =$false} #secondly, make sure it only contains numbers and it's between 0-254 if ($bValidIP) { [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic") | Out-Null foreach ($item in $arrSections) { if (!([Microsoft.VisualBasic.Information]::isnumeric($item))) {$bValidIP = $false} } } if ($bValidIP) { foreach ($item in $arrSections) { $item = [int]$item if ($item -lt 0 -or $item -gt 254) {$bValidIP = $false} } } Return $bValidIP } function Validate-SubnetMask ($strSubnetMask) { $bValidMask = $true $arrSections = @() $arrSections +=$strSubnetMask.split(".") #firstly, make sure there are 4 sections in the subnet mask if ($arrSections.count -ne 4) {$bValidMask =$false} #secondly, make sure it only contains numbers and it's between 0-255 if ($bValidMask) { [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic") | Out-Null foreach ($item in $arrSections) { if (!([Microsoft.VisualBasic.Information]::isnumeric($item))) {$bValidMask = $false} } } if ($bValidMask) { foreach ($item in $arrSections) { $item = [int]$item if ($item -lt 0 -or $item -gt 255) {$bValidMask = $false} } } #lastly, make sure it is actually a subnet mask when converted into binary format if ($bValidMask) { foreach ($item in $arrSections) { $binary = [Convert]::ToString($item,2) if ($binary.length -lt 8) { do { $binary = "0$binary" } while ($binary.length -lt 8) } $strFullBinary = $strFullBinary+$binary } if ($strFullBinary.contains("01")) {$bValidMask = $false} if ($bValidMask) { $strFullBinary = $strFullBinary.replace("10", "1.0") if ((($strFullBinary.split(".")).count -ne 2)) {$bValidMask = $false} } } Return $bValidMask } function ConvertTo-Binary ($strDecimal) { $strBinary = [Convert]::ToString($strDecimal, 2) if ($strBinary.length -lt 8) { while ($strBinary.length -lt 8) { $strBinary = "0"+$strBinary } } Return $strBinary } function Convert-IP-To-Binary ($strIP) { $strBinaryIP = $null if (Validate-IP $strIP) { $arrSections = @() $arrSections += $strIP.split(".") foreach ($section in $arrSections) { if ($strBinaryIP -ne $null) { $strBinaryIP = $strBinaryIP+"." } $strBinaryIP = $strBinaryIP+(ConvertTo-Binary $section) } } Return $strBinaryIP } Function Convert-SubnetMask-To-Binary ($strSubnetMask) { $strBinarySubnetMask = $null if (Validate-SubnetMask $strSubnetMask) { $arrSections = @() $arrSections += $strSubnetMask.split(".") foreach ($section in $arrSections) { if ($strBinarySubnetMask -ne $null) { $strBinarySubnetMask = $strBinarySubnetMask+"." } $strBinarySubnetMask = $strBinarySubnetMask+(ConvertTo-Binary $section) } } Return $strBinarySubnetMask } Function Convert-BinaryIPAddress ($BinaryIP) { $FirstSection = [Convert]::ToInt64(($BinaryIP.substring(0, 8)),2) $SecondSection = [Convert]::ToInt64(($BinaryIP.substring(8,8)),2) $ThirdSection = [Convert]::ToInt64(($BinaryIP.substring(16,8)),2) $FourthSection = [Convert]::ToInt64(($BinaryIP.substring(24,8)),2) $strIP = "$FirstSection`.$SecondSection`.$ThirdSection`.$FourthSection" Return $strIP } #End Functions #Main $BinarySubnetMask = (Convert-SubnetMask-To-Binary $SubnetMask).replace(".", "") $BinaryNetworkAddressSection = $BinarySubnetMask.replace("1", "") $BinaryNetworkAddressLength = $BinaryNetworkAddressSection.length $CIDR = 32 - $BinaryNetworkAddressLength $iAddressWidth = [System.Math]::Pow(2, $BinaryNetworkLength) $iAddressPool = $iAddressWidth -2 $BinaryIP = (Convert-IP-To-Binary $IP).Replace(".", "") $BinaryIPNetworkSection = $BinaryIP.substring(0, $CIDR) $BinaryIPAddressSection = $BinaryIP.substring($CIDR, $BinaryNetworkAddressLength) #Starting IP $FirstAddress = $BinaryNetworkAddressSection -replace "0$", "1" $BinaryFirstAddress = $BinaryIPNetworkSection + $FirstAddress $strFirstIP = Convert-BinaryIPAddress $BinaryFirstAddress #End IP $LastAddress = ($BinaryNetworkAddressSection -replace "0", "1") -replace "1$", "0" $BinaryLastAddress = $BinaryIPNetworkSection + $LastAddress $strLastIP = Convert-BinaryIPAddress $BinaryLastAddress Write-Host "IP Adddress: $IP" -ForegroundColor Yellow Write-Host "SubnetMask: $SubnetMask" -ForegroundColor Yellow Write-Host "" Write-Host "Starting IP: $strFirstIP" -ForegroundColor Green Write-Host "Ending IP: $strLastIP" -ForegroundColor Green