Get IP Data

I was doodling in PowerShell this morning and ended up with what I hope is a useful function to retrieve IP configuration information, sort of like IPCONFIG, but using WMI. The beauty is that I can connect to remote machines and the output is an object which leads to all sorts of possibilities.

My function is called Get-IPData.

The function takes computernames as a parameter, uses WMI to get the NetworkAdapterConfiguration for IP and DHCP enabled adapters.

The rest of the function simply selects key properties including another WMI query to get the associated adapater’s speed. Because some items like IP address are stored as arrays, I use a hash table expression to return the first item only.

I started to do my usual code polishing but then I realized this might be a good exercise for you. The function works as is with some limited error handling in a Try/Catch block. But there are several areas for improvement or enhancement. You might want to:

  • Add support for alternate credentials
  • Add comment based help
  • Add support for running as a background job
  • Create a graphical, WinForm/WPF version
  • Add IPv6 information
  • Add the average ping response time
  • Add more adapter information

I’ll leave the rest to you. This could be a nice warmup practice for the upcoming Scripting Games.

Download Get-IPData.ps1

10 thoughts on “Get IP Data”

  1. You don’t need the loop, one of the neat things about GET-WMIObject is Computer name can be array of names.

    1. Guess I just have the habit so that I can either pipe names to the function

      “computer1″,”computer2″,”computer3″ | Get-IPData

      Or pass them as a parameter value

      Get-IPData “computer1″,”computer2″,”computer3″

      But in this case, the ForEach loop is redundant.

  2. After testing, if I was only calling Get-WMIObject you are correct. But because I have other things going on like some Write-Verbose messages, those fail so I need the ForEach loop.

  3. !. Does NOT require Admin credentaials.
    2. Will NOT work for adapters with static addresses.

    Nice utility for gathering NIC data.

  4. I’m getting this error:

    WARNING: A positional parameter cannot be found that accepts argument ‘System.Object[]‘.

      1. Hi,

        The error is because there’s a small error on line 19. You’ve written “Select Description,DNSHostname” twice in a row.
        It works brilliantly if you remove the second one.

        Regards, Vincent

  5. Good eyes. That mistake was only in the posted code. It is not in the script text file you download. You have to be careful copying and pasting code samples which is why I always include a text file you can download. The function continues to work for me.

    1. Yeah you’re right. The download link is a welcome change to most script sites. But because of that I still have the subconscious habit to copy on the site itself.

Comments are closed.