/
Convert-SubnetToBitMask.ps1
46 lines (40 loc) · 1.24 KB
/
Convert-SubnetToBitMask.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
function Convert-SubnetToBitmask {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$subnet
,
[Parameter(Mandatory = $false)]
[int]$cidr
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
Test-ValidateSingle -validParams ('subnet','cidr') @PSBoundParameters
New-Variable -Name 'result' -Scope 'Private' -Value ('')
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
try {
<#
if (($null -eq $subnet) -and ($null -eq $cidr)) {
$subnet = Get-Subnet # get it from local ip
}
#>
if ($null -eq $cidr) {
$cidr = Get-Cidr -subnet $subnet
}
if ($cidr -lt 0 -or $cidr -gt 32) {
throw "CIDR value ($cidr) is out of the valid range (0-32)"
}
$result = ('1' * $cidr).PadRight(32, '0')
}
catch {
throw $_ # Propagate the error
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# test: Convert-SubnetToBitmask -cidr 24
}