
The Chairman has returned with a new challenge that will test not only your PowerShell skills but your networking knowledge as well. The challenge has been graded for Intermediate and Advanced levels, but don’t let that stop you.
Depending on how you approach the challenge, you may find it easier to break it down into discrete parts.
Using PowerShell, write a function, or set of functions, that will test if an IPv4 address like 172.16.2.33/16 belongs to the 172.16.0.0 network. You should test your code with addresses that go beyond the /16 or /24 network boundaries. Or you might come at this from another angle. What is the subnet mask for an address like 172.16.10.20/17? And what network does that represent?
You will have to decide how to accept parameter values for the IP Address, subnet and/or subnet mask. Advanced users should include Verbose output showing the process. Advanced users should also provide 2 parameter sets. One to display a boolean value if the address belongs to the subnet and another to provide detailed information.
The Chairman looks forward to reading your comments with links to your solutions.
I’m afraid I cheated a bit. a couple of years ago I had to write a script to install a monitoring application that required this functionality. It’s configured destination for reporting was dependent on which subnet the host machine was on. After a lot of research and borrowing code online, this is what I came up with:
https://github.com/cgdii/PowershellScripts/blob/7f6bf59e1e94bd9f73a19ab0b9666a8f6bb05f36/IPChallenge.ps1
Here’s the output:
PS C:\Scripts\Users\cadavis\IronScripter> .\IPChallenge.ps1
IP Address 172.16.2.33/16 being tested against 172.16.0.0/16
IP Address 172.16.2.33/16 is in Subnet 172.16.0.0/16
—————————-
IP Address 172.18.2.33/16 being tested against 172.16.0.0/16
IP Address 172.18.2.33/16 is NOT in Subnet 172.16.0.0/16
—————————-
IP Address 172.16.10.20/17 being tested against 172.16.0.0/16
IP Address 172.16.10.20/17 is in Subnet 172.16.0.0/16
My solution here:
https://gist.github.com/nmbell/8f19dce3b4902be28960e3800f242a40
Here’s my take on it: https://github.com/jhendricks123/IronScripter.IPChallenge
Some example output:
PS C:\> Test-IPAddress -Address 10.125.0.16/17 -Verbose
VERBOSE: Parsed subnet mask as 17 in CIDR notation
VERBOSE: Creating decimal representation subnet mask from the value 17 in preparation for bitwise operations
VERBOSE: The decimal value of the subnet mask is 4294934528
VERBOSE: Converting 10.125.0.16 to decimal value in preparation for bitwise operations
VERBOSE: The decimal value of the IPAddress is 175964176
VERBOSE: Calculating the NetworkAddress value by performing a binary and operation on the IPAddress and the SubnetMask
VERBOSE: Calculating the BroadcastAddress value by performing a binary exclusive or on the NetworkAddress and an inverted SubnetMask
IPAddress : 10.125.0.16
SubnetMask : 255.255.128.0
NetworkAddress : 10.125.0.0
BroadcastAddress : 10.125.127.255
FirstHostAddress : 10.125.0.1
LastHostAddress : 10.125.127.254
MaximumHosts : 32766
IsNetworkAddressCorrect : True