Giter VIP home page Giter VIP logo

dnsclient-ps's Introduction

DnsClient-PS

A cross-platform DNS client for PowerShell utilizing the DnsClient.NET library.

DNS query options in PowerShell and the native .NET class library have always been 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.

DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups. This module attempts to expose its power in a PowerShell native manner in order to automate DNS tasks without needing to parse the output of utilities like nslookup and dig. However, it is not intended to be a general replacement for those utilities.

Notable Features

  • Fully supported cross platform
  • Low level access to request/response protocol details
  • Optional dig'like human readable output
  • Optional response cache for performance sensitive tasks

Install

Release

The latest release version can found in the PowerShell Gallery or the GitHub releases page. Installing from the gallery is easiest using Install-Module from the PowerShellGet module. See Installing PowerShellGet if you don't already have it installed.

# install for all users (requires elevated privs)
Install-Module -Name DnsClient-PS -Scope AllUsers

# install for current user
Install-Module -Name DnsClient-PS -Scope CurrentUser

NOTE: If you use PowerShell 5.1 or earlier, Install-Module may throw an error depending on your Windows and .NET version due to a change PowerShell Gallery made to their TLS settings. For more info and a workaround, see the official blog post.

Development

To install the latest development version from the git main branch, use the following PowerShell command. This method assumes a default PSModulePath environment variable.

# install latest dev version
iex (irm https://raw.githubusercontent.com/rmbolger/DnsClient-PS/main/instdev.ps1)

Quick Start

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

The -QueryType and -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 :<port>.

# 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 -Recursion, -Timeout, and -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 Set-DnsClientSetting.

# 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

Requirements and Platform Support

  • Supports Windows PowerShell 5.1 or later (Desktop edition) with .NET Framework 4.7.1 or later.
  • Supports PowerShell 7.0 or later (Core edition) on all supported OS platforms.
  • PowerShell 6.x should also work, but I won't be actively testing against it.

Changelog

See CHANGELOG.md

dnsclient-ps's People

Contributors

rmbolger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dnsclient-ps's Issues

The 'Address' property under the 'Answer'/'RecordData' seems to be inaccessible

$(Resolve-Dns -NameServer $dnsServer -Query $FQDNsToQueryFor  | Select-Object -Expand Answers)                          

DomainName                         TimeToLive RecordClass RecordType RecordData
----------                         ---------- ----------- ---------- ----------
api.monitor.azure.com.             192        IN          CNAME      api.privatelink.monitor.azure.com.
api.privatelink.monitor.azure.com. 9          IN          A          SCRUBBED

However when trying to reference the 'RecordData' property/column I get a null value

$(Resolve-Dns -NameServer $dnsServer -Query $FQDNsToQueryFor  | Select-Object -Expand Answers).RecordData

I wanted to do some further digging and tried the following command:

$((Resolve-Dns -NameServer $dnsServer -Query $FQDNsToQueryFor  | Select-Object -Expand Answers) | Select-Object -Property *)                             

CanonicalName     : api.privatelink.monitor.azure.com.
DomainName        : api.monitor.azure.com.
RecordType        : CNAME
RecordClass       : IN
TimeToLive        : 63
InitialTimeToLive : 63
RawDataLength     : 18

Address           : SCRUBBED
DomainName        : api.privatelink.monitor.azure.com.
RecordType        : A
RecordClass       : IN
TimeToLive        : 0
InitialTimeToLive : 0
RawDataLength     : 4

So then I tried to reference the 'Address' Property of the Object in the Array, but got a weird Result...

$((Resolve-Dns -NameServer $dnsServer -Query $FQDNsToQueryFor  | Select-Object -Expand Answers) | Select-Object -Property *).Address

OverloadDefinitions
-------------------
System.Object&, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e Address(int )

I did a little digging and found that the property Name for 'Address' might cause some problems, it doesn't seem to be a Reserved word, but the Address METHOD seems to supersede calling the property? I don't know if I am using proper terminology as I am newer to C# and PowerShell scripting, but you may be able to understand.

Here is the relevant PowerShell issue that is actually still open.
PowerShell/PowerShell#8105

Not working on Windows PowerShell

When I tested the module, it works in PowerShell 7 but fails in Windows PowerShell.
With any cmdlet, I get this exception:

Exception calling "QueryServer" with "2" argument(s): "Query 59746 =>
8.8.8.8.in-addr.arpa. IN PTR on 192.168.2.1:53 failed with an error."
At C:\Users\tobia\OneDrive\Dokumente\WindowsPowerShell\Modules\DnsClient-
PS\1.1.0\Public\Resolve-Dns.ps1:78 char:17
+                 $client.QueryServer($nsList, $qst)
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationExce
   ption
    + FullyQualifiedErrorId : DnsResponseException

resolve-dns throw a powershell error with non existent domain

As the tile said, the module throw a powershell error and not a "inexistent domain" with an inexistent domain.

Ex:

⮞  resolve-dns xttox.com`
MethodInvocationException: C:\Users\exp1x835\OneDrive - Beneva\Documents\PowerShell\Modules\DnsClient-PS\1.1.1\Public\Resolve-Dns.ps1:78`

Line |
  78 |                  $client.QueryServer($nsList, $qst)
     |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "QueryServer" with "2" argument(s): "Query 42931 => xttox.com IN A on
     | 96.45.46.46:53 timed out or is a transient error."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.