Home > Powershell > PSMag PowerShell Brain Teaser: finding list of active IP Addresses of Local Machine

PSMag PowerShell Brain Teaser: finding list of active IP Addresses of Local Machine

November 28th, 2012 Leave a comment Go to comments

Today i read the brain teaser winner announcement at Powershell Mag, that I am the winner of the last week’s Brain Teaser. And the same is announced in next Brain Teaser post here :

 

Here is the description of the last brain teaser:

 

And my solution was this:

The cmdlet gives a complete IPAddress Object(s):

 

There are other options as well, a  nice one mentioned by Mike F. Robbins is [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().UnicastAddresses|% Address - read the post at PSMag for a complete list of answers and comments.

I am happy to be the winner, and awaiting for the book “Powershell 3.0 First Look” to give it a try.

————————–

I would like to expand this post to share my learning from participating in this brain teaser, below I will make note of the same.

 

How to find Members of a DotNet Class from Powershell:

[Net.DNS] | Get-Member – This gives the members of the host’s Runtime

lets try this:  [Net.DNS] | Get-Member * -Force

This is still showing Runtime Members

Now lets try this, this must work: [Net.DNS] | Get-Member -Static

Note: There are obsolete methods for Net.Dns class which are recommended to be avoided by Developers, not sure should it be followed in Powershell as well.

System.Net.DNS Class:

According to MSDN:

Provides simple domain name resolution functionality.

The Dns class is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS).

The host information from the DNS query is returned in an instance of the IPHostEntry class. If the specified host has more than one entry in the DNS database, IPHostEntry contains multiple IP addresses and aliases.

Few Methods of interest from this class:

  • [Net.DNS]::GetHostAddresses()  – Returns IP Addreses for the specified host.
  • [Net.DNS]::GetHostByName() – Obsolete. Gets the DNS information for the specified DNS host name
  • [Net.DNS]::GetHostByAddress() – Obsolete. Creates an IPHostEntry instance from IP Address
  • [Net.DNS]::GetHostEntry() – Resolves an IP address or Hostname to an IPHostEntry instance
  • [Net.DNS]::GetHostName()  – Gets the host name of the local computer
  • [Net.DNS]::Resolve() – Obsolete. Resolves a DNS host name or IP address to an IPHostEntry instance

 

You can check the overload definition (What parameters, and how to pass them) which can be found by executing the Method without the parenthesis

 

Examples of few useful static methods from Net.Dns class:

GetHostAddresses:

This method gives all the IP Address (IPv4 and IPv6) assigned to the machine right now.

Even this “[net.dns]::GetHostAddresses($null)” will work for local machine.

 

GetHostEntry:

This method gives All the IP Address list of the machine provided by Name or IP Address.

It gives similar output as GetHostAddresses.

If the host name or IP Address provided cannot be found an error is thrown.

 

GetHostName:

This is a simple method, it just gives you  the local machine name.

Its very useful, I was trying to get a cmdlet for this to stop using hostname.exe

In all these cases we can run them on remote machines as well by using Powershell remoting, but that’s not required as how this works – we get the results by giving a remote machine IP Address or Machine Name.

 

That’s it for now, Enjoy the Power Of Shell!

 

Categories: Powershell Tags:
  1. No comments yet.
*