Functions/Networking/Convert-SubnetMasktoCIDR.ps1
function Convert-SubnetMaskToCIDR { [cmdletbinding()] Param() DynamicParam { # Get list of Subnet Masks $Masks = Get-SubnetMask # Instantiate Runtime Parameter Dictionary, Attach Runtime Parameters, and return $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary.Add('SubnetMask', (New-DynamicParameter -ParamName 'SubnetMask' -ValueType string[] -Dataset $Masks -DefaultValue $Masks -Mandatory $False -ValueFromPipeLine:$True)) return $RuntimeParameterDictionary } Process { # Convert Runtime Parameter Dictionary into Available Constants foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value} foreach ($SNM in [array]$SubnetMask) { $TrueIP = try{[Net.IPAddress]::Parse($SNM)}catch{$null} if($TrueIP) { $BinaryMask = Convert-SubnetMaskToBits -SubnetMask ([string]$TrueIP.IPAddressToString) $CIDR = [int](($BinaryMask.split('0',2))[0].length) $CIDR } else {Write-Host "Could not convert MASK ($SNM) to CIDR" -ForegroundColor Red} } } } |