A PowerShell IP Challenge

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.


One Reply to “A PowerShell IP Challenge”

  1. Carl Davis

    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

Leave a Reply

Your email address will not be published. Required fields are marked *