Functions/Networking/Convert-CIDRtoSubnetMask.ps1
function Convert-CIDRtoSubnetMask { [cmdletbinding()] Param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidateRange(0,32)] [int] $CIDR ) Process { $BinaryMask = ("1"*$cidr+"0"*(32-$cidr)) $INT64 = [convert]::ToInt64(($BinaryMask),2) $IPAddress = Convert-Int64toIP -INT64 $INT64 } End { $TrueIP = try{[Net.IPAddress]::Parse($IPAddress)}catch{$null} if($TrueIP) {$TrueIP.ipaddresstostring} else {Write-Host "Could not convert CIDR ($CIDR) and parse resultant IP ($IPAddress)" -ForegroundColor Red} } } |