How to Check DNS Records Type- A, NS, TXT, CNAME, MX using Powershell

If you are an IT administrator, you may require checking DNS various records using Nslookup command or any other third party website https://mxtoolbox.com/, In this my blog I'm going to use a simple Powershell script to check some common DNS records.




Prerequisites -

  • Windows Powershell
  • Powershell script ExecutionPolicy to RemoteSigned
Step 1- Enable Script Execution on PowerShell.
By-Default PowerShell doesn't allow execution of scripts, let enable script execution followed by the command.
PS C:\Users\Amar> Set-ExecutionPolicy RemoteSigned -Force
Step 2- Copy the below script and save as DNS_Check.PS1
Set-ExecutionPolicy RemoteSigned -force
Write-Host "WELCOME TO Domain Lookup !!!" -ForegroundColor Yellow
$domainname= Read-Host "Enter or Paste Domain Name" 
$domainRecord= Read-Host "A / NS / MX / txt / CNAME ?" 
# To Check domain A Record information
if ($domainRecord -eq 'A')
    { Write-Host Checking A record for the Domain- $domainname -ForegroundColor Green
        nslookup -type=a $domainname         
}

# To Check domain NS Record information
elseif ($domainRecord -eq 'NS') 
        { Write-Host Checking NS record for the Domain- $domainname -ForegroundColor Green
        nslookup -type=ns $domainname 
}

  # To Check domain MX Record information
elseif($domainRecord -eq 'MX') 
        { Write-Host Checking MX record for the Domain- $domainname -ForegroundColor Green
        nslookup -type=mx $domainname
}

 # To Check domain TXT Record information
elseif ($domainRecord -eq 'txt')
        { Write-Host Checking TXT record for the Domain- $domainname -ForegroundColor Green
        nslookup -type=txt $domainname
}

 # To Check domain CNAME Record information
 elseif($domainRecord -eq 'CNAME')   
        { Write-Host Checking CNAME record for the Domain- $domainname -ForegroundColor Green
        nslookup -type=cname $domainname
}

  # Print Errro when DNS Record not match
else { ($domainRecord -notmatch 'A', 'NS', 'MX', 'txt', 'CNAME')
        { Write-Host "No match Domain recored found, Please enter Valid Domain Record." -ForegroundColor Red
      }

}


 #sleep -Seconds 60
 Read-Host "Press Enter key to exit..."
exit
Copy all the above code and paste into notepad save as DNS_Check.PS1
Step 3- Test Script by checking a domain information.
3A- Check A Record for domain google.com, Right Click on DNS_Check.PS1 file and choose Run with PowerShell.
On the next screen, enter domain to get the detail of DNS record.
3B- Check NS record. To Check NS record for any domain run the script with PowerShell, Enter Domain name and choose NS record type. as in picture below.
The same process you can follow to check other DNS records.
Cheers!!

No comments:

Post a Comment