Problem with DNS name resolution when using System.Net.DNS class
I recently ran into a problem when writing a PowerShell script to perform DNS Name resolution using .NET class System.Net.DNS (http://msdn.microsoft.com/en-us/library/system.net.dns.aspx).
I noticed when I’m using System.Net.DNS to perform reverse lookup (GetHostByAddress method), even though the PTR record is missing in DNS, it is still able to resolve the name. It looks like this method connects to the host to retrieve its host name.
When the machine is powered off, GetHostByAddress method is unable to resolve the IP address to it’s name (Which is desired result because there is no PTR record in reverse lookup zone):
I then powered on the machine (Jump01), now I get different result. the IP address has been resolved to the host name:
Because of the inconsistent result, I am not able to use System.Net.DNS class (even there are few other methods in this class, but the results are all the same).
What I really need is a way to perform DNS name resolution and provides SAME result as using nslookup. I found several PowerShell community projects, but none of them suits my requirement:
dnsutil.ps1:
Source: http://gsexdev.blogspot.com/2006/09/powershell-dns-utility-script-for.html.
Reason: C# code wrapped inside the PowerShell script. Only works on 32bit PowerShell console.
C# .NET DNS query component:
Source: http://www.codeproject.com/KB/IP/dnslookupdotnet.aspx
Reason: Does not perform PTR (reverse) lookup.
DnDns.dll:
Source & Guide: http://securitythroughabsurdity.com/2008/02/dndns-net-dns-client-library-resolver.html
Reason: It does not perform what it claims to do. I.e. The DnsQueryRequest.Resolve method only takes 4 parameters, not 5 as what instruction says. Also, it does not resolve IP to name. (for example, it does not resolve 192.168.1.26, but it resolves 26.1.168.192.in-addr.arpa).
DNSShell:
Source & Guide: http://dnsshell.codeplex.com/
Reason: this works great and it’s easy to use. can perform reverse lookup using a single PowerShell Cmdlet Get-DNSRecord. However, I need to place the source to %PSModulePath% and import the module in PowerShell. I cannot do this to the production server which I use to run my script.
At the last, I found a freeware called JHSoftware.DnsClient and it solved my problem!
As shown above
- I first load the dll into Powershell
[System.Reflection.Assembly]::LoadFile("<path-to-DLL>")
-
nslookup, make sure the PTR record does not exist
-
use the LookupReverse method from JHSoftware.DNSClient class the perform reverse lookup on 192.168.1.26 (JUMP01). it could not find it and an error was thrown.
[JHSoftware.DNSClient]::LookupReverse("192.168.1.26")
- Modify the PowerShell command, used Try-Catch statement, produced more user friendly output.
Try {[JHSoftware.DNSClient]::LookupReverse("192.168.1.26")} Catch {"PTR Record not found!"}
- use same method (LookupReverse) to perform reverse lookup on one of my SCCM server, it successfully returned the FQDN.
This is to do with my DNS record check which is part of my SCCM Health Check script that I posted in previously. I am rewriting the DNS records check in the script as it produces inaccurate result. I have already completed the ability of utilizing PowerShell Remoting to check inboxes backlogs as I mentioned in the previous post. I will post the updated SCCM Health Check script in the next few days.
Leave a comment