PowerShell : Resolve DNS Hostname to IP and Reverse using a Single function
HOW IT WORKS :
Use the System.Net Namespace‘s DNS ( System.Net.Dns ) class and its function ( getHostEntry() ) to resolve the Host to IPAdress resolution and Vice-Versa
NAMESPACE : System.Net
ASSEMBLY : System (in System.dll)
FUNCTION : GetHostentry
WHAT IS System.Net NAMESPACE ?
Classes in the namespace can be used to develop Windows Store apps or desktop apps. To know more on System.Net namespace and its classes, click here .
GET THE SCRIPT : Click here to go to my GitHub gist to get the code.
HOW TO USE IT :
Well, you can use this function in multiple ways, by default it resolves IP addresses to Host Names using the DNS query. But if you want to Resolve Host name to IP Address use the switch -HostnameToIP to do the reverse lookup
Here are some examples of its usage –
- You can resolve a list of IP Addresses to host names
- Or, you can pass a file with list of IP Addresses to get there host names resolved
- Similarly you can pass multiple Host names and get there respective IP addresses resolved
NOTE : Do not forget the –HostnameToIP switch for the reverse lookup
- the function returns the Data in form of objects and you can access its attributes using Dot ( . ) operator
- Use the -FlushDNS switch to Flush the DNS Resolver cache from your system before making the DNS query
Hope you find it useful, Happy learning !
Thank you for stopping by 🙂