Powershell check if ip or subnet belong to each other

Powershell

I needed a powershell script today with which i can check if two given IP addresses match or if a given IP address belongs to a subnet or if a smaller subnet belongs to a larger one (or vise vursa). I found a nice script written by Sava from http://www.padisetty.com/ which had part of the functionality i required so i took and modified it to suit my needs. Below you will be able to find the modified script i hope it helps somebody :). The script will return an array of two values, one to indicate true or false and the second the direction.  The direction is important as you may want to compare values for a firewall and as such you want to fit one in the other in a particular direction.

Usage example:

  • checkSubnet ‘10.185.255.128/26’ ‘10.165.255.166/32’
  • checkSubnet ‘10.125.255.128’ ‘10.125.255.166′
  • checkSubnet ‘10.140.20.0/21’ ‘10.140.20.0/27’