Resolve-DNSName Usage in Powershell
Resolve-DNSName is a new entry in Windows 8 and Server 2012 to replace our old friend Nslookup. Nslookup is still a great tool to query the DNS servers, but it has its limitations. For example, it is not easy to use inside the scripts. While these days, automation is very important part of every tool. Powershell introduces Resolve-DNSName that gives much more flexibility and options while querying the DNS Servers.
How Name Resolution Works:
The DNS resolution works in the following order:
- Hosts file is checked for any entry
- Local cached is checked for any cached records
- DNS Server is queried for name resolution
First it checks the hosts file for an entry for the queried domain, then it checks for the local cache, if the domain already queried or not and finally it quires the DNS Server, that is responsible for that domain.
Resolve-DNSName (Query As You Want):
For this example i have made an entry in the hosts file for AdExchangeAdmin.com with the IP 10.0.0.1
After adding the hosts file entry, now as soon i am querying the domain from my PC, it is returning the local IP.
Here comes the fun part. We can mention to skip the hosts file by using “NoHostsFile” option to skip the hosts file check and move to the 2nd option. We can also mention“CacheOnly” to use cache or “DNSOnly” to directly query the DNS and skip Hosts and Cache.
After using the NoHostsFile, it skips the host file and gets the actual IPs instead of the fake 10.0.0.1 entry that i made in the hosts file.
Resolve-DNSName Record Types:
As a Systems Admin, i often require to get different types of DNS records, especially MX information for the domains. Now i can do that in Powershell using Type parameter in Resolve-DNSName command as follows:
Here i used the Resolve-DNSName command to fetch the MX record for my domain, ADExchangeAdmin.com
To list the DNS Servers that hold the record of any domain, we can use the “NS” (Short for Name Server). The output looks like this:
When we have multiple DNS Servers having record for any domain, we can query any specific DNS also for the record by specifying the Server. Here, i used 126.96.36.199 Server IP from my previous output.
Similarly, we can query any type of records using Reolve-DNSName.
Finally, we can use the flexibility of Powershell to pipe the output to the Export-CSV and save the it in CSV format.
You can use “Get-Help Resolve-DNSName” to get detailed help for this command in Powershell or visit Technet documentation to learn more about it.