Public/New-IPCalcIPAddress.ps1
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<#
.SYNOPSIS New-IPCalcIPAddress returns an IPAddress object for the corresponding input. .DESCRIPTION New-IPCalcIPAddress returns an IPAddress object for the corresponding input. It accepts an IP address string in dotted decimal notation (0.0.0.0) or an array of four bytes ([byte[]]@(0,0,0,0)). .PARAMETER IPAddressString IP address string in dotted decimal notation (0.0.0.0) .PARAMETER IPAddressBytes Array of four bytes ([byte[]]@(0,0,0,0)) .EXAMPLE New-IPCalcIPAddress -IPAddressString 192.168.0.1 Address : 16820416 AddressFamily : InterNetwork ScopeId : IsIPv6Multicast : False IsIPv6LinkLocal : False IsIPv6SiteLocal : False IsIPv6Teredo : False IsIPv4MappedToIPv6 : False IPAddressToString : 192.168.0.1 .EXAMPLE New-IPCalcIPAddress -IPAddressBytes [byte[]]@(192,168,0,1) Address : 16820416 AddressFamily : InterNetwork ScopeId : IsIPv6Multicast : False IsIPv6LinkLocal : False IsIPv6SiteLocal : False IsIPv6Teredo : False IsIPv4MappedToIPv6 : False IPAddressToString : 192.168.0.1 .INPUTS System.String System.Byte[] .OUTPUTS System.Net.IPAddress .NOTES This function is a wrapper around the System.Net.IPAddress class constructor and limits it to the creation of IPv4 Addresses only. #> function New-IPCalcIPAddress { [CmdletBinding(DefaultParameterSetName='String',PositionalBinding=$false)] param ( [Parameter(Position=0,ParameterSetName='String',Mandatory=$true)] [ValidateScript({ if($_ -notmatch '^([01]?\d?\d|2[0-4]\d|25[0-5])\.([01]?\d?\d|2[0-4]\d|25[0-5])\.([01]?\d?\d|2[0-4]\d|25[0-5])\.([01]?\d?\d|2[0-4]\d|25[0-5])$'){ throw "String $_ was not a valid IP address" } else{ $true } })] [string]$IPAddressString, [Parameter(Position=0,ParameterSetName='ByteArr',Mandatory=$true)] [ValidateRange(0,255)] [ValidateCount(4,4)] [byte[]]$IPAddressBytes ) begin { } process { switch ($PSCmdlet.ParameterSetName) { 'String' { [ipaddress]::Parse($IPAddressString) } 'ByteArr' { New-Object -TypeName ipaddress -ArgumentList (,$IPAddressBytes) } } } end { } } |