Skip to content

Instantly share code, notes, and snippets.

@jhochwald
Last active March 25, 2016 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhochwald/168bca5498853d4084d1 to your computer and use it in GitHub Desktop.
Save jhochwald/168bca5498853d4084d1 to your computer and use it in GitHub Desktop.
Takes a IP as a string and returns the same IP address as a binary string with no decimal points
#region License
<#
{
"info": {
"Statement": "Code is poetry",
"Author": "Joerg Hochwald",
"Contact": "joerg.hochwald@outlook.com",
"Link": "http://hochwald.net",
"Support": "https://github.com/jhochwald/MyPowerShellStuff/issues"
},
"Copyright": "(c) 2012-2016 by Joerg Hochwald & Associates. All rights reserved."
}
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
By using the Software, you agree to the License, Terms and Conditions above!
#################################################
# modified by : Joerg Hochwald
# last modified : 2016-03-25
#################################################
#>
#endregion License
Function Global:Convert-IPToBinary {
<#
.SYNOPSIS
Converts an IP address string to it's binary string equivalent
.DESCRIPTION
Takes a IP as a string and returns the same IP address as a binary string with no decimal points
.PARAMETER IP
The IP address which will be converted to a binary string
.EXAMPLE
PS C:\> Convert-IPToBinary -IP '10.211.55.1'
Binary IPAddress
------ ---------
00001010110100110011011100000001 10.211.55.1
Converts 10.211.55.1 to it's binary string equivalent 00001010110100110011011100000001
.NOTES
Works with IPv4 addresses only!
#>
[CmdletBinding(ConfirmImpact = 'None',
SupportsShouldProcess = $true)]
[OutputType([psobject])]
param
(
[Parameter(ValueFromPipeline = $true,
Position = 1,
HelpMessage = 'The IP address which will be converted to a binary string')]
[ValidateNotNullOrEmpty()]
[ValidatePattern('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')]
[Alias('IPAddress')]
[String]$IP
)
BEGIN {
$Binary = $null
$Result = $null
$SingleIP = $null
}
PROCESS {
foreach ($SingleIP in $IP) {
try {
$SingleIP.split(".") | %{ $Binary = $Binary + $([convert]::toString($_, 2).padleft(8, "0")) }
} catch [System.Exception] {
Write-Error -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -ErrorAction Stop
# Still here? Make sure we are done!
break
# Aw Snap! We are still here? Fix that the Bruce Willis way: DIE HARD!
exit 1
} catch {
Write-Error -Message "Could not convert $SingleIP!" -ErrorAction Stop
# Still here? Make sure we are done!
break
# Aw Snap! We are still here? Fix that the Bruce Willis way: DIE HARD!
exit 1
}
$Result = new-object PSObject -Property @{
IPAddress = $SingleIP
Binary = $Binary
}
}
}
END {
Return $Result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment