Part IV: PowerShell Multithreading – Asyncronous Network and Host Discovery Scanner

Part III of my Get-SecNetMap “Mini-Module”:

  1. Get-SecNetMap
  2. Get-SecPortScan
  3. Get-SecIPRange
  4. Convert-SecIPAddress (This Post)
  5. Get-SecArpTable

You can download the module source files here:


The Convert-SecIPAddress function is actually pretty cool. I was able to use mathematics learned from my cryptography courses during my undergrad to convert an IP address to an integer and vice-verse. It is performance optimized and has support for long integers which was where most of the errors I had during development were. Since many of the integers returned are 10char in length, I had to force cast them into LONG type so Powershell could actually work. This conversion was to allow the randomization of IP addresses in the Get-SecNetMap function. In order to randomize, I had to convert each IP to an integer so that I could “shuffle” the integers and use the Get-Random accordingly. Then the conversion from Int back to an IP address would take place. Lets take a look at the sample below.



[sourcecode language=”powershell” wraplines=”false” collapse=”false”]
Function Convert-SecIPAddress {
Converts an IP Address to an Integer and vice versa

Performs mathmatical operations on each octect to determine the integer representation of an IP address.
Also will calculate an IP address from an Integer.

Switch to allow script to process an IP address into an integer

Switch to allow script to process an integer into an IP address

Convert-SecIPAddress -toINT


Convert-SecIPAddress -fromINT 3232235877

Name: Convert-SecIPAddress.ps1
Author: hackerbryan
Version: 1.0
Param (



[Parameter(Position=0,Mandatory = $True,ParameterSetName=”toINT”)]

[Parameter(Position=0,Mandatory = $True,ParameterSetName=”fromINT”)]


# Declare Output Array
[email protected]()

# Convert the IP to a byte array
if ($toINT) {[byte[]]$ByteArray = $IPAddr.GetAddressBytes()}



# Convert from IP Address to Integer
If ($toINT){

for ($ci=0; $ci -lt $ByteArray.Length; $ci++) {
$Int64+=[System.Int64]::Parse(($ByteArray)[$ci]) % 256 * [Math]::Pow(256, $Power)

} $OutputArray += $Int64

} # End If toINT

# Convert from Integer to IP Address
ElseIf ($fromINT) {

[math]::truncate($Int64%256)) -join “.”

$OutputArray += $IP.ToString()

} # End ElseIf fromINT

} # End Process


Return $OutputArray


} #End Convert-SecIPAddress

Tags// ,