Advertisement
7Script

Powershell Subnetting Functions

Jul 7th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# 
  2.     .NOTES
  3.     ===========================================================================
  4.      Created with:  Windows PowerShell ISE
  5.      Created on:    7/7/2015 10:34 PM
  6.      Created by:    David Septimus  
  7.     ===========================================================================
  8.     .DESCRIPTION
  9.         Functions for validating subnet and wildcard masks.
  10.         Both functions return a boolean value if the input string is valid or nothing if the string is invalid.
  11. #>
  12.  
  13. function Validate-SubnetMask
  14. {
  15.     param (
  16.         [cmdletbinding()]
  17.         [string]$Mask
  18.     )
  19.     $stringMask = $i = $validAddress = $null
  20.     if ([System.Net.IPAddress]::TryParse($Mask, [ref]$validAddress))
  21.     {
  22.        
  23.         $validAddress = $validAddress.GetAddressBytes()
  24.         $validAddress |
  25.         %{
  26.             $i++
  27.             [string]$stringMask += [convert]::ToString([int32]$_, 2).PadLeft(8,'0')
  28.         }
  29.         for ($i = 0; $i -lt $stringMask.Length ; $i++)
  30.         {
  31.             if ($stringMask[$i] -ne $null)
  32.             {
  33.                 if ($i -eq 0 -and [int32]::Parse($stringMask[$i]) -eq 0)
  34.                 {
  35.                     break
  36.                 }
  37.                 elseif ($i -gt 0 -and (([int32]::Parse($stringMask[$i - 1]) -eq 0) -and ([int32]::Parse($stringMask[$i]) -eq 1)))
  38.                 {
  39.                     Write-Output $false
  40.                     break
  41.                 }
  42.             }
  43.             if ($i -eq $stringMask.Length-1)
  44.             {
  45.                 Write-Output $true
  46.                 break
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. function Validate-WildcardMask
  53. {
  54.     param (
  55.         [cmdletbinding()]
  56.         [string]$Mask
  57.     )
  58.     $validAddress = $null
  59.     if ([System.Net.IPAddress]::TryParse($Mask, [ref]$validAddress))
  60.     {
  61.         #reverse the input string
  62.         $validAddress = ($validAddress.IPAddressToString).Split("`.")
  63.         [array]::Reverse($validAddress)
  64.         $reversedMask = $validAddress -join '.'
  65.        
  66.         $validAddress = [System.Net.IPAddress]::Parse([string]$reversedMask)
  67.        
  68.  
  69.         #validate the reversed mask
  70.         $stringMask = $i = $null
  71.         $validAddress = $validAddress.GetAddressBytes()
  72.         $validAddress |
  73.         %{
  74.             $i++
  75.             [string]$stringMask += [convert]::ToString([int32]$_, 2).PadLeft(8,'0')
  76.         }
  77.         for ($i = 0; $i -lt $stringMask.Length ; $i++)
  78.         {
  79.             if ($stringMask[$i] -ne $null)
  80.             {
  81.                 if ($i -eq 0 -and [int32]::Parse($stringMask[$i]) -eq 0)
  82.                 {
  83.                     break
  84.                 }
  85.                 elseif ($i -gt 0 -and (([int32]::Parse($stringMask[$i - 1]) -eq 0) -and ([int32]::Parse($stringMask[$i]) -eq 1)))
  86.                 {
  87.                     Write-Output $false
  88.                     break
  89.                 }
  90.             }
  91.             if ($i -eq $stringMask.Length-1)
  92.             {
  93.                 Write-Output $true
  94.                 break
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement