Public/New-IPCalcNetwork.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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<#
.SYNOPSIS New-IPCalcNetwork returns an object that represents a network. .DESCRIPTION New-IPCalcNetwork returns an object that represents a network. The resulting object will contain the IP address identifying the network (AKA the subnet ID), the subnet mask for the network, and the mask length (AKA CIDR suffix). .PARAMETER NetworkID IP address string in dotted decimal notation (0.0.0.0) of an IP address in the network to be returned. If this is not the actual network ID, the function will calculate the correct IP and return it as part of the resulting object. .PARAMETER NetworkMask Network mask string in dotted decimal notation (0.0.0.0) of the network to be returned. If this is not a valid subnet mask, the function will error. .PARAMETER NetworkMaskLength Integer in the range 0 - 32 representing the mask length of the network to be returned. .EXAMPLE New-IPCalcNetwork -NetworkID 192.168.1.0 -NetworkMask 255.255.255.0 Returns an object representing the new network with the provided IP address and Mask .EXAMPLE New-IPCalcNetwork -NetworkID 192.168.1.0 -NetworkMaskLength 24 Returns an object representing the new network with the provided IP address and Mask of the specified length .INPUTS System.String System.Int32 .OUTPUTS System.Management.Automation.PSCustomObject .NOTES This function is intended to be used as a tool for network planning. It should help planners quickly split networks and determine if a particular IP address belongs to that network. #> function New-IPCalcNetwork { [CmdletBinding(DefaultParameterSetName='NetworkMask',PositionalBinding=$false)] param ( [Parameter(Position=0,ParameterSetName='NetworkMask',Mandatory=$true)] [Parameter(Position=0,ParameterSetName='NetworkMaskLength',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]$NetworkID, [Parameter(Position=1,ParameterSetName='NetworkMask',Mandatory=$true)] [ValidateScript({$_ -in $(GetValidMask)})] [string]$NetworkMask, [Parameter(Position=1,ParameterSetName='NetworkMaskLength',Mandatory=$true)] [ValidateRange(0,32)] [int]$NetworkMaskLength ) begin { } process { $OBJMask = switch ($PSCmdlet.ParameterSetName){ 'NetworkMask' { [ipaddress]$NetworkMask } 'NetworkMaskLength' { $(GetValidMask)[($NetworkMaskLength)] } } $NetworkObj = New-Object -TypeName psobject -Property @{ ID = GetNetworkID -IPAddress $NetworkID -Mask $OBJMask MaskLength = $(GetValidMask).IndexOf($OBJMask) } | Add-Member -PassThru -MemberType ScriptProperty -Name 'Mask' -Value {$(GetValidMask)[$This.MaskLength]} | Add-Member -PassThru -MemberType ScriptProperty -Name 'BroadcastAddress' -Value {GetNetworkBroadcast -IPAddress $this.ID -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptProperty -Name 'AddressClass' -Value {GetIPAddressClass -IPAddress $this.ID} | Add-Member -PassThru -MemberType ScriptProperty -Name 'IsPrivate' -Value {TestIPAddressIsPrivate -IPAddress $this.ID} | Add-Member -PassThru -MemberType ScriptProperty -Name 'MaxAddressCount' -Value {GetMaxAddressCount -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptProperty -Name 'MaxHostCount' -Value {GetMaxAddressCount -Usable -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptProperty -Name 'MaxSubnetCount' -Value {GetMaxSubnetCount -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptProperty -Name 'FirstHost' -Value {GetFirstNetworkAddress -IPAddress $this.ID -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptProperty -Name 'LastHost' -Value {GetLastNetworkAddress -IPAddress $this.ID -MaskLength $this.MaskLength} | Add-Member -PassThru -MemberType ScriptMethod -Name 'ToString' -Value {'{0}/{1}' -f $this.ID.IPAddressToString,$this.MaskLength} -Force | Add-Member -PassThru -MemberType ScriptMethod -Name 'Split' -Value { param([int]$SubnetCount) SplitNetwork -IPAddress $this.ID -MaskLength $this.MaskLength -SubnetCount $SubnetCount | ForEach-Object { New-IPCalcNetwork -NetworkID $_['IPAddress'] -NetworkMaskLength $_['MaskLength'] } } | Add-Member -PassThru -MemberType ScriptMethod -Name 'ContainsIP' -Value { param([IPAddress]$IPAddress) TestNetworkContainsIP -IPAddress $this.ID -MaskLength $this.MaskLength -ChildIPAddress $IPAddress } $NetworkObj.psobject.TypeNames.Insert(0,'IPCalcNetwork') $NetworkObj } end { } } |