Yesterday I was a bit bored and started writing some random scripts.
The first one was a small script to convert an IP address to a decimal value…
Although I don’t need this often, there are still times when it is useful.
So here it is 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
function Convert-IPtoDecimal { <# .SYNOPSIS Converts an IP address to decimal. .DESCRIPTION Converts an IP address to decimal value. .EXAMPLE PS D:\> IPAddressToDecimal -IPAddress '127.0.0.1','192.168.0.1','10.0.0.1' Decimal IPAddress ------- --------- 2130706433 127.0.0.1 3232235521 192.168.0.1 167772161 10.0.0.1 .EXAMPLE PS D:\> IPAddressToDecimal '127.0.0.1','192.168.0.1','10.0.0.1' Decimal IPAddress ------- --------- 2130706433 127.0.0.1 3232235521 192.168.0.1 167772161 10.0.0.1 .EXAMPLE PS D:\> '127.0.0.1','192.168.0.1','10.0.0.1' | IPAddressToDecimal Decimal IPAddress ------- --------- 2130706433 127.0.0.1 3232235521 192.168.0.1 167772161 10.0.0.1 #> [cmdletbinding()] param ( [parameter(mandatory=$true,position=0,valuefrompipeline=$true)][String[]]$IPAddress ) begin { } process { [String[]] $IP = $IPAddress.Split('.') $Object = New-Object -TypeName psobject -Property (@{ 'IPAddress' = $($IPAddress); 'Decimal' = [Int64]( ([Int32]::Parse($IP[0]) * [Math]::Pow(2, 24) + ([Int32]::Parse($IP[1]) * [Math]::Pow(2, 16) + ([Int32]::Parse($IP[2]) * [Math]::Pow(2, 8) + ([Int32]::Parse($IP[3]) ) ) ) ) ) }) $Object } end { } } |
Hi jeff
If I studied Computer Science Can I, I became a professional in programming?
Hi Bandar,
Sure, you can do anything if you truly put your mind to it.
I know of a beautician that went and became a c# developer. 😉
Jeff.