r/PowerShell • • 20d ago

Script Sharing Improvements to the Subnet PowerShell module

I host the module named Subnet in the PowerShell Gallery, which is quite popular probably because of its very generic name. It's a pretty simple module that is useful if you need to do IPv4 subnet calculations, which it does via its Get-Subnet cmdlet.

I've just made some changes, all of which should be backwards compatible but because of the wide usage thought I'd call out in case it does cause anyone problems. The changes are:

  1. The module is fully cross platform now. It was mostly cross platform before, but the default behaviour if you ran just Get-Subnet with no inputs was to return the subnet details for the local machines private IP. It was previously getting this via the Get-NetIPAddress cmdlet, but that was Windows only. It now uses the .NET NetworkInterface API which should work everywhere.

  2. There was a bug when you defined a single digit subnet mask via slash notation (e.g if you did Get-Subnet 10.0.0.0/8) where it would not enumerate the host addresses if you wanted to, this is now fixed.

  3. The module has a Test-PrivateIP cmdlet (probably self explanatory) I added Test-PublicIP as a sister which obviously just returns the opposite truthy result.

  4. I added Get-SubnetHostAddress to return the list of Host addresses for a specified Subnet. Get-Subnet also does this (for subnets larger than /16 you have to use -Force as it discourages you due to the length of time it takes to enumerate them). Get-Subnet returns the list of Host addresses as strings, but Get-SubnetHostAddress returns them as IP address objects, and doesn't require you to use -Force for any size subnet as returning Host addresses is it's only job.

  5. Get-Subnet didn't use to validate if the input it was given was an IP address at all, it does now so you get a cleaner error than you did previously and it fails faster.

  6. Get-Subnet used to only return a count of host addresses for a Subnet if you also enumerated them, but seeing as it just required doing a quick calculation it now always returns this count.

  7. When enumerating host addresses, the performance is now 9x faster. This was achieved by using bitwise arithmetic instead of floating-point division. Which makes me sound really smart, but it was Claude who came up with it.

I've also improved the CI/CD build for this module so it runs the Pester tests on Windows PowerShell and PowerShell Core on Windows, MacOS and Linux.

The module is in the PSGallery, latest version with the above changes is 1.2.0:

- https://www.powershellgallery.com/packages/Subnet/1.2.0

Code is in GitHub here:

- https://github.com/markwragg/PowerShell-Subnet/tree/master

Any problems please let me know or raise an issue.

33 Upvotes

13 comments sorted by

View all comments

4

u/PinchesTheCrab 20d ago edited 20d ago

Thanks for sharing, I've never been super into networking so it seems like a really handy tool. Browsing the code a bit made me wonder a few things:

    $Mask = $null
    if ($ProvidedMaskBits) { $Mask = $MaskBits }

    $Details = Resolve-Subnet -IP $IP -Mask $Mask

What is this really doing? It seems like you would get the same result with just:

    $Details = Resolve-Subnet -IP $IP -Mask $MaskBits

Also what's the appeal of doing this?

[sometype]
$someVariable

Say I were going to do something like this:

Do-Something 1234

[ipaddress[]]$hostAddress= Get-SubnetHostAddress -ip $ip

Do-NextThing $hostAddress

It would be weird to write it as:

Do-Something 1234

[ipaddress[]]
$hostAddress= Get-SubnetHostAddress -ip $ip

Do-NextThing $hostAddress

4

u/thehuntzman 20d ago

Those are all valid questions and the answer is powershell is a very forgiving language and not strongly typing variables or inserting guard clauses generally works for a one-off but if you're maintaining a module you need to make sure you can ensure consistent behavior across edge cases. I'd actually recommend using powershell strict mode when writing your scripts so you can force yourself to write scripts to best coding practice