-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-IpV4Network.ps1
60 lines (52 loc) · 1.95 KB
/
Get-IpV4Network.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
function Get-IpV4Network {
<#
.SYNOPSIS
Determine the IPv4 network given an ip address and a subnet mask
.DESCRIPTION
Determine the IPv4 network given an ip address and a subnet mask
.PARAMETER IpAddress
An IP address (or array of addresses) that you want to determine the network of
.PARAMETER SubnetMask
An IP subnet mask. Defaults to: '255.255.255.0'
.PARAMETER IncludeInput
Switch to determine if you want the input as part of the output
.EXAMPLE
Get-IpV4Network -IpAddress 10.10.10.200 -SubnetMask 255.255.255.128
10.10.10.128
.EXAMPLE
Get-IpV4Network -IpAddress 10.10.10.200, 192.168.1.32 -SubnetMask 255.255.255.0 -IncludeInput
IpAddress SubnetMask Network
--------- ---------- -------
10.10.10.200 255.255.255.0 10.10.10.0
192.168.1.32 255.255.255.0 192.168.1.0
#>
[CmdletBinding(ConfirmImpact = 'None')]
Param(
[Parameter(Mandatory, HelpMessage = 'Please enter an IpAddress in the form a.b.c.d', ValueFromPipeline, Position = 0)]
[alias('Address')]
[ipaddress[]] $IpAddress,
[alias('Sn')]
[ipaddress] $SubnetMask = '255.255.255.0',
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($item in $IpAddress) {
$Result = ([ipaddress] ($item.address -band $SubnetMask.address)).IPAddressToString
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
IpAddress = $item.IPAddressToString
SubnetMask = $SubnetMask.IPAddressToString
Network = $Result
})
} else {
$Result
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}