Saturday, December 8, 2012

"I love the new PowerShell 3 cmdlets". Part 1


Previously to PowerShell 3 this was the easiest way to resolve a hostname.

$IPAdr = [string][]::gethostaddresses(""])

This is fine except many native .NET calls seem to be a minefield of memort leaks and slow performance. Also there are some obvious limitations on flexibility, for example you would have to wrap this is more code if you only wanted an IPv4 address (this will return both).

Enter PowerShell 3...............!

Here is the same deal using the new 'Resolve-DNSName' cmdlet.

$IP = (Resolve-DnsName "" -type A)

Much nicer, much faster and much more flexible. True to the PowerShell architecture, this returns a .NET object. Let's explore an example. When I try '' the cmdlet returns:

Name                Type    TTL    Section    IPAddress
----                ----    ---    -------    ---------      AAAA    201    Answer     2607:f8b0:400e:c02::68      A       201    Answer      A       201    Answer      A       201    Answer      A       201    Answer      A       201    Answer      A       201    Answer

Interesting to see Google's IPv6 AAAA record.

So one of the properties is IP4Address which would return the following string:

Have fun exploring it!


No comments:

Post a Comment