Saturday, December 8, 2012

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

Resolve-DNSName

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

$IPAdr = [string][system.net.dns]::gethostaddresses("myhost.mydomain.net"])

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 "myhost.mydomain.net" -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 'www.google.com' the cmdlet returns:

Name                Type    TTL    Section    IPAddress
----                ----    ---    -------    ---------
www.google.com      AAAA    201    Answer     2607:f8b0:400e:c02::68
www.google.com      A       201    Answer     74.125.129.106
www.google.com      A       201    Answer     74.125.129.105
www.google.com      A       201    Answer     74.125.129.104
www.google.com      A       201    Answer     74.125.129.99
www.google.com      A       201    Answer     74.125.129.103
www.google.com      A       201    Answer     74.125.129.147

Interesting to see Google's IPv6 AAAA record.

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

74.125.129.106
74.125.129.105
74.125.129.104
74.125.129.99
74.125.129.103
74.125.129.147

Have fun exploring it!

Cheers!

No comments:

Post a Comment