Just shipped a brand new module called DnsClient-PS. It’s a cross-platform DNS client for PowerShell utilizing the DnsClient.NET library. In the library author’s own words:
DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups. It can be used in any kind of application to query the network’s DNS server or any other DNS server even on non-default ports.
For some inexplicable reason, DNS query options in PowerShell and the native .NET class library have always been rather disappointing. Resolve-DnsName is a decent addition, but it’s only available on Windows and doesn’t seem to be headed cross-platform anytime soon. The System.Net.Dns namespace is also extremely limited in its capabilities.
With DnsClient-PS, I’m attempting to expose DnsClient.NET’s power in a PowerShell native manner and be able to automate DNS tasks without needing to parse the output of utilities like
dig. However, it is not intended to be a general replacement for those utilities.
The primary function is
Resolve-Dns and requires a
-Query parameter that accepts one or more string values. This defaults to an A record lookup against your OS configured DNS server(s).
Resolve-Dns -Query google.com Resolve-Dns google.com Resolve-Dns 'google.com','www.google.com' 'google.com','www.google.com' | Resolve-Dns
-NameServer parameters are the other two common ones you’ll generally use. NameServer can take an array with IP addresses or FQDNs. Each one can also have an explicit port specified by appending
# Do an AAAA lookup Resolve-Dns google.com -QueryType AAAA Resolve-Dns google.com AAAA # Do an SRV lookup against a domain controller Resolve-Dns _gc._tcp.contoso.com SRV -NameServer dc1.contoso.com Resolve-Dns _gc._tcp.contoso.com SRV -ns dc1.contoso.com,dc2.contoso.com Resolve-Dns _gc._tcp.contoso.com SRV -ns 192.168.0.1:53,dc2.contoso.com:53
The output of a successful query is a DnsQueryResponse object. Its raw form isn’t very human readable, but it’s quite comprehensive in the detail it provides about the response. If all you care about are the answers, you will want to do something like this.
Resolve-Dns google.com | Select-Object -Expand Answers (Resolve-Dns google.com).Answers
Keep in mind that answers for different record types are also different object types with different properties. For example, notice the differences between the following:
Resolve-Dns google.com a | Select-Object -Expand Answers | Get-Member Resolve-Dns google.com txt | Select-Object -Expand Answers | Get-Member Resolve-Dns google.com soa | Select-Object -Expand Answers | Get-Member
There are a number of optional parameters that can alter various settings for a query such as
-UseTcpOnly. These can be set on a per-call basis using the parameters available in
Resolve-Dns or they can be set as new defaults for the current session using
# Disable recursion and change the timeout for this call only Resolve-Dns google.com -ns ns1.google.com -Recursion:$false -Timeout (New-Timespan -Sec 30) # Change the settings for all queries in this session Set-DnsClientSettings -ns ns1.google.com -Recursion:$false -Timeout (New-Timespan -Sec 30) Resolve-Dns google.com # Check the current session settings Get-DnsClientSettings
- Initial Release
- Added functions