In the world, everything works around DNS records to find where stuff is coming from and going to. As an email admin, you are constantly checking records for clients or updating them so checking what records exist is something that for me I do all the time.

Many companies lockdown the internet so you cannot always get to the places to check a record or the admin that manages DNS is not available.

I put together a PowerShell script that not only checks External but Internal DNS records. Here is an example of the external records:

Quickly check dns records with the help of powershell

I tested with Bing, Google, Yahoo, my own domains and they all provide info. Here is an example of the internal domain:

Quickly check dns records with the help of powershell

SCRIPT

The PowerShell script can be used in multiple ways. You can modify two line items from PowerShell ISE and run it or you can save the script and run it by specifying the domain and record type. On line 4 and 5, the following needs to be changed:

  • $domain – specify the domain you want to query, can be an external one such as Gooogle.com or internal one such as tlab.com
  • $recordType – specify the record type you interested in, it may be an “A” record, “CNAME” record etc.

I specified the record types in capital letters. Here is the script that you can use:

#Change the domain to the one that you want to check
#Enter in a record type such as: A, CNAME, TXT
param (
    [string]$domain = "google.com",
    [string]$recordType = "A"
)

function Check-DNSRecord {
    param (
        [string]$domain,
        [string]$recordType
    )

    try {
        $dnsResult = Resolve-DnsName -Name $domain -Type $recordType -ErrorAction Stop
        Write-Output "DNS record found for $domain ($recordType):"
        $dnsResult | Format-Table -AutoSize
    } catch {
        Write-Error "Error: $_"
    }
}

#Usage:
Check-DNSRecord -domain $domain -recordType $recordType

Here is an example of the above showing a CNAME record directly within PowerShell ISE:

Quickly check dns records with the help of powershell

If you want to rather save the script and run it from PowerShell directly, it does not matter then what is specified in the script as you will use the following command to generate the output:

.\CheckDNS.ps1 -domain "bing.com" -recordType "A"

The above line of code, calls the function in the PowerShell script, after that you use the “-domain” option to specify the domain and lastly you need to use the “-recordType” option to search the record you want.

The script was tested on Windows Server Core and GUI in the Exchange Management Shell (EMS), PowerShell and PowerShell ISE.

Hope you find it helpful.

Discover more from Everything-PowerShell

Subscribe now to keep reading and get access to the full archive.

Continue reading