It’s been a while since I’ve posted, and since it’s almost Christmas I thought I’d better get on with it.

Without further ado I want to post NetShell (I’m not very good at coming up with imaginative names). NetShell is a collection of 17 functions and a few supporting functions in a script module.

The functions described here have been updated since posting this article. The updated functions are available as part of the script module Indented.NetworkTools.

Installation is a manual process, but not too hard. Open up DocumentsWindowsPowerShellModules, extract the ZIP file. Make sure it includes the NetShell folder or it won’t work. The module is not currently signed, something else on the to-do list. Once it’s there, Import-Module NetShell and off you go.

It includes the following:

ConvertTo-BinaryIP Converts an IP address into a binary string
ConvertTo-Byte A supporting function, a simple conversion of a string to a byte array (ASCII encoding)
ConvertTo-DecimalIP Converts an IP address to 32-bit decimal number
ConvertTo-DottedDecimalIP Converts a binary or 32-bit decimal back to an IP
ConvertTo-Mask Converts from a mask length. For example, gets you from 22 to 255.255.252.0
ConvertTo-MaskLength Converts from the IP form of a mask to the length
ConvertTo-String Another supporting function, converts a byte array to a string (ASCII encoding)
Get-BroadcastAddress Returns the broadcast address for the specified IP address and subnet mask
Get-NetworkAddress Returns the network address for the specified IP address and subnet mask
Get-NetworkRange Returns every IP within the specified range
Get-NetworkSummary Everything about an IP address and mask I considered useful
New-DhcpDiscoverPacket A supporting function for Send-DhcpDiscover. Creates the packet to send (a large byte array)
New-Socket Creates an instance of System.Net.Sockets.Socket, an arbitrary network socket to do with as you please.
New-SysLogDateTime A supporting function to create a DateTime string in the format SysLog likes.
Read-DhcpOption A supporting function to read off an Option from a DHCP packet. Needs to be fed the Extended.BinaryReader class at the top of the module.
Read-DhcpPacket Creates and uses an instance of Extended.BinaryReader to process a DHCP packet and translate the fields.
Receive-Bytes Receives a stream of bytes from the network using a socket
Remove-Socket Cleans up after New-Socket
Send-Bytes Sends an arbitrary byte array over the network using a socket
Send-DhcpDiscover Creates and sends a DHCPDISCOVER packet, then processes and returns the response
Start-Syslog Starts a SysLog server. No termination for this one at the moment. Needs a bit more work.
Test-Smtp Does the SMTP test you normally wind up doing with telnet, returning all the results along with the SMTP banner.
Test-SysLogDateTime A supporting function to check the format of a DateTime that may or may not be present in a SysLog message.
Test-SysLogPRI A supporting function to test of the PRI value in a SysLog message.
Test-TcpPort Returns a boolean indicating whether or not the port connection succeeded.

4 Comments

  1. Nice work, Chris. At least you called the module “NetShell” rather than “PowerShell.” Redundancy avoidance win! 😉

    Reply

  2. I know it’s been a while since u posted this page but…THANK YOU!

    Reply

  3. I’m using the commands acquired from http://www.indented.co.uk/index.php/2010/01/23/powershell-subnet-math/

    The functions outlined in that page work pretty much as designed.
    Tested using –> Get-NetworkSummary 10.2.2.0/23

    I have my concerns with the scripts from that page and I have modified the original functions to correct them.
    I also see that the code is a little different and maybe you’ve taken steps to correct that.
    The concern was for a /32 subnet.
    Tested using –> Get-NetworkSummary 10.2.2.50/32
    The hosts would show -2 and the range specified 10.2.2.51 – 10.2.2.49.

    I did however want to report the toubles I’m seeing with an unmodified downloaded/imported module.
    If I get around to it, I may try to debug it…but as of yet, I have not.

    When I run the below command with the downloaded/imported module, I am getting errors:
    Get-NetworkSummary 10.2.2.0/23

    Get-NetworkSummary : Cannot process argument transformation on parameter ‘IPAddress’. Cannot convert value
    “10.2.2.0/23” to type “System.Net.IPAddress”. Error: “An invalid IP address was specified.”
    At line:1 char:20
    + Get-NetworkSummary 10.2.2.0/23
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-NetworkSummary], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-NetworkSummary

    I also tried without the /23 which then asks for the SubnetMask but doesn’t accept the MaskLength instead.
    Cannot convert value “” to type “System.Net.IPAddress”. Error: “An invalid IP address was specified.”
    At C:Windowssystem32WindowsPowerShellv1.0ModulesNetShellNetShell.psm1:308 char:7
    + $IPAddress = $Temp[0]
    + ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation

    ConvertTo-Mask : Cannot validate argument on parameter ‘MaskLength’. The argument is null, empty, or an element of the
    argument collection contains a null value. Supply a collection that does not contain any null values and then try the
    command again.
    At C:Windowssystem32WindowsPowerShellv1.0ModulesNetShellNetShell.psm1:309 char:36
    + $SubnetMask = ConvertTo-Mask $Temp[1]
    + ~~~~~~~~
    + CategoryInfo : InvalidData: (:) [ConvertTo-Mask], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,ConvertTo-Mask

    Specifying the SubnetMask “255.255.254.0” when prompted or running the following command also generates errors but still seems to provide accurate results:
    Get-NetworkSummary 10.2.2.0 255.255.254.0
    Cannot convert value “” to type “System.Net.IPAddress”. Error: “An invalid IP address was specified.”
    At C:Windowssystem32WindowsPowerShellv1.0ModulesNetShellNetShell.psm1:308 char:7
    + $IPAddress = $Temp[0]
    + ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation

    ConvertTo-Mask : Cannot validate argument on parameter ‘MaskLength’. The argument is null, empty, or an element of the
    argument collection contains a null value. Supply a collection that does not contain any null values and then try the
    command again.
    At C:Windowssystem32WindowsPowerShellv1.0ModulesNetShellNetShell.psm1:309 char:36
    + $SubnetMask = ConvertTo-Mask $Temp[1]
    + ~~~~~~~~
    + CategoryInfo : InvalidData: (:) [ConvertTo-Mask], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,ConvertTo-Mask
    Class : A
    NetworkAddress : 10.2.2.0
    HostRange : 10.2.2.1 – 10.2.3.254
    BroadcastDecimal : 167904255
    NetworkDecimal : 167903744
    SubnetMask : 255.255.254.0
    IsPrivate : True
    BroadcastAddress : 10.2.3.255
    NumberOfHosts : 510
    MaskLength : 23

    Reply

    • It expects either:

      Get-NetworkSummary 10.2.2.0 255.255.254.0

      Or:

      Get-NetworkSummary -Network 10.2.2.0/23

      There are two distinct parameter sets, the first uses position to read an IP Address and Subnet Mask separately.

      The next version will fix this difficulty 🙂

      Chris

      Reply

Leave a Reply

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