As an IT professional we use lot of times commands like nslookup to resolve a hostname from an ip address or vice versa.
Nslookup it's a very useful command for the DNS queries. However it returns only CNAME , A ,AAAA Records and can't use it in a script.
Today we can use Powershell commands that can resolve dns queries for different Record Types.
Until Powershell 4 you had to use the [System.Net.Dns] to resolve dns queries.
Let's see how can use the Resolve-DnsName and the [System.Net.Dns]
How to query the DNS Server to resolve the IP Address
Resolve-Dnsname has more additional parameters than nslookup that can give you more control.
If you want to resolve the IP Address of a domain then you should type:
Resolve-DnsName -Name www.askme4tech.com
The -Server parameter specify from which DNS Server do the request.
Resolve-DnsName -Name askme4tech.com -Server 220.127.116.11
Something very useful is the -NoHostsFile parameter.
When you query to resolve the IP Address your DNS Client use the following order:
- Local HOSTS file
- DNS Client Resolver Cache
- DNS server query
Now let's say that you would like to skip Step 1. Then you can use the -NoHostsFile parameter.
As you can see with the -NoHostsFile you have Ip Address from a Dns Server and not from the Hosts File/.
Resolve-DnsName can use it from Powershell 4 and above.
If you want to resolve the IP Address of a domain from earlier version of Powershell 4 then you should user the [System.Net.Dns]
When you type the command you will receive different information like Address,AddressFamily,ScopeID and more.
Now let's say that you don't need all these information but only the IP Addresses. Just add the .ipaddresstostring at the end.
How to query the DNS Server to resolve the Hostname
Now let's say that you need to resolve the Hostname from the IP Address.
Then you can type and receive the results.
Resolve-DnsName -Name 18.104.22.168
You can use the same parameters as above.
Of course a similar command you can found in earlier versions of Powershell 4.
How to resolve different types of DNS Records
Except from the A Records you can resolve and other Types. Some of them are:
A_AAAA = 0, the DNS query type is A_AAAA.
- -- AAAA = 28, the DNS query type is IPv6 server address.
- -- NS = 2, the DNS query type is name server.
- -- MX = 15, the DNS query type is mail routing information.
- -- MD = 3, the DNS query type is mail destination.
- -- MF = 4, the DNS query type is mail forwarder.
- -- CNAME = 5, the DNS query type is canonical name.
- -- SOA = 6, the DNS query type is start of authority zone.
If you need more details you can read the Microsoft Docs Resolve-DnsName
So let's find the Name Servers of the askme4tech.com. You can use the -Type parameter
Resolve-DnsName -Name www.askme4tech.com -Type ns
Now let's sat that you want to find the MX Records of Outlook.com or validate the MX Records of the Mail Server in your company.
Resolve-DnsName -Name OUTLOOK.COM -Type MX
Or verify the spf records from my Domain.
Resolve-DnsName -Name askme4tech.com -Type txt
Finally the Resolve-DnsName it's a powerful command with lot of parameters to use when you have DNS issues.