Get Public Dns Record Value PowerShell
Hello,
Nslookup is great, but it’s an old exe tool, and PowerShell is the new way to go. Thanks to .Net, we go the System.Net.dns class, with two methods:
- GetHostByName
- Resolve
But none of these allow us as much flexibility as nslookup do. You can specify a record type, you can specify which DNS server to use.
Be cause of those needs, we need another solution, a friend of mine, Thomas, showed a web service named “DNS LG” which offer that kind of features.
If you look at the documentation, you can see that the API is quite easy to use:
Let’s try with a domaon of ours:
http://www.dns-lg.com/google1/itfordummies.net/txt
We can see a JSON formatted answer, let’s try to use this with PowerShell:
Invoke-WebRequest -Uri "http://www.dns-lg.com/google1/itfordummies.net/txt" | Select-Object -ExpandProperty Content | ConvertFrom-Json | Select-Object -ExpandProperty answer
And voilĂ !
You can use the API will all of those kind of DNS record types:
- A
- AAAA
- CERT
- CNAME
- DHCID
- DLV
- DNAME
- DNSKEY
- DS
- HINFO
- HIP
- IPSECKEY
- KX
- LOC
- MX
- NAPTR
- NS
- NSEC
- NSEC3
- NSEC3PARAM
- OPT
- PTR
- RRSIG
- SOA
- SPF
- SRV
- SSHFP
- TA
- TALINK
- TXT
I wrote a basic function to use this API with more ease, you can find it on my GitHub repo:
https://github.com/edemilliere/Misc/blob/master/Get-PublicDnsRecord.ps1
You can load the function without downloading the file with:
Invoke-Expression (Invoke-WebRequest -Uri https://raw.githubusercontent.com/edemilliere/Misc/master/Get-PublicDnsRecord.ps1 | Select-Object -ExpandProperty Content)
You can use it without specifying a node and it will select one randomly from the list available on the API:
You can also choose one, the auto-completion will work on this, and on record type too.
Hereunder an example on how to use the function to check some Office 365 DNS prerequisites: