This is just a short note about Get-DNS (my dig for PowerShell cmdlet). Someone emailed me to ask how they could specify the dns servers, and the answer was that you couldn’t. So then I looked a bit deeper, and realized that the code I copied from CodeProject already had support for that, so I whipped up a new version which you can download here (source here under GPL).
The only real difference is the addition of a -DnsServers parameter which accepts one or more dns server ip addresses. NOTE: they must be IP Addresses, not domain names (sorry, I was lazy, Maybe if someone I’ll make sure that gets tweaked eventually). You may include the port (eg: 71.6.202.220:53) but you don’t have to unless it’s non-standard (ie: not port 53). You may specify multiple servers if you need a fallback … and as always, if you leave it off, the cmdlet will use your default DNS servers.
Have fun, and uhm, feel free to tip the waiter.
So, today someone came to #PowerShell on irc.freenode.net and asked how to do nslookup functionality natively in PowerShell, and nobody knew how … and then as the question evolved, it turned out that what they needed was to do a lookup for both MX and TXT records for a series of domains …
To cut a long story short, I took some code from CodeProject and whipped up Get-DNS and a PoshNet snapin module which you can download here (source here under GPL).
It has an -EmulateDig switch to produce output similar to what you would see from the dig command, and otherwise produces an object with all the information in it. I’ll be cleaning this up in the future, but for now the main use is quite simple:
Get-Dns [-Name] <String> [[-Type] <QueryType[]>] [[-Class] <QueryClass>] [-DnsServers <String[]>] [-EmulateDigOutput]
The mandatory parameter is the domain Name of the computer to look up, of course … which you can pass on the pipeline. Pretty much every DNS record type ever created is allowed, including dozens I’ve never heard of — you can get a list by calling Get-DNS -Type ""
And you can specify the class as IN, CS, CH, HS, or ANY (the default).
[new] In this new release (Nov 20, 2008) you have the -DnsServers parameter which accepts one or more dns server ip addresses. NOTE: they must be IP Addresses, not domain names (sorry, I was lazy, Maybe if someone I’ll make sure that gets tweaked eventually). You may include the port (eg: 71.6.202.220:53) but you don’t have to unless it’s non-standard (ie: not port 53). You may specify multiple servers if you need a fallback … and as always, if you leave it off, the cmdlet will use your default DNS servers.
On CTP2 you can install this by just calling Add-Module .PoshNet.dll (or by putting it in a folder … DocumentsWindowsPowerShellPackagesPoshNet and then you can just Add-Module PoshNet). On the PowerShell 1.0 release, you need to use InstallUtil.exe to install it, so I’ve included a “Install.ps1” script which should let you call: .\Install.ps1 PoshNet.dll from an elevated console:
You can also ask for multiple record types at once, to solve the original question that was asked on IRC:
Of course, the other part of the request is that you need to be able to query a whole list of servers, so you can do that, for instance, by having a file with a domain per line, or whatever. Also, for the sake of this last example, recall that unlike the linux “dig” command, we can output everything as an object (like we did above) and all of the Records* types are collections of objects, so you can expand them, and enumerate or format them. Play with the “Answers” collection: