Skip to content

Instantly share code, notes, and snippets.

@matt40k
Last active November 23, 2022 10:28
Show Gist options
  • Save matt40k/03410719493279800d642134ae94cb2e to your computer and use it in GitHub Desktop.
Save matt40k/03410719493279800d642134ae94cb2e to your computer and use it in GitHub Desktop.
Uses DNS over HTTPS endpoint to lookup A records - PowerShell
$domain = 'cloudflare.com'
$url = 'https://cloudflare-dns.com/dns-query?name='+$domain+'&type=A';
$header = @{"accept"="application/dns-json"}
$response = (Invoke-WebRequest -Uri $url -Headers $header -UseBasicParsing).Content
$r = [System.Text.Encoding]::UTF8.GetString($response) | ConvertFrom-Json
$r
foreach ($item in $r.Answer)
{
Write-Host $item
}
$domain = 'google.com'
$url = 'https://dns.google.com/resolve?name='+$domain + '&type=A';
$r = (Invoke-WebRequest -Uri $url -UseBasicParsing).Content | ConvertFrom-Json
foreach ($item in $r.Answer)
{
Write-Host $item
}
$domain = "quad9.net"
$url = "https://dns.quad9.net:5053/dns-query?name=$($domain)"
$r= (Invoke-WebRequest -Uri $url -UseBasicParsing).Content | ConvertFrom-Json
foreach ($item in $r.Answer)
{
Write-Host $item
}
@corey-alford
Copy link

corey-alford commented Aug 31, 2022

To fix the CloudFlare script update this line:
$r = $response | ConvertFrom-Json
To this:
$r = [System.Text.Encoding]::UTF8.GetString($response) | ConvertFrom-Json

Also I would just output recommend getting rid of the foreach and just putting $r.Answer so it stays an array, making it easier export to file and it also outputs in a table which is easier to read.

@corey-alford
Copy link

Can't say if the Quad9 one will work because the port is blocked outbound where I am out atm, but this allows you to run it as a function and just select which provider you want. Also accepts multiple domains at once as a array.
Resolve-DnsSSL -Domains <Domains Array> -Provider <CloudFlare, Google, Quad9>

 Function Resolve-DnsSSL {
	 param (
	 [Parameter(Mandatory=$True)]
	 [array]$domains,
	 
	 [Parameter()]
	 [ValidateSet("CloudFlare","Google","Quad9")]
	 [String]$Provider = "CloudFlare"
	 )
	 
	 Foreach ($domain in $domains) {
		 
		 switch ($Provider) {
			 "CloudFlare" {$url = 'https://cloudflare-dns.com/dns-query?name='+$domain+'&type=A'; break}
			 "Google"     {$url = 'https://dns.google.com/resolve?name='+$domain + '&type=A'; break}
			 "Quad9"      {$url = "https://dns.quad9.net:5053/dns-query?name=$($domain)"; break}
		 }
		 
		 
		 
		 $header = @{"accept"="application/dns-json"}
		 $response = (Invoke-WebRequest -Uri $url -Headers $header -UseBasicParsing)
		 
		 if ($response.Content.GetType().name -eq "Byte[]") {
			 $json = [System.Text.Encoding]::UTF8.GetString($response.Content)
		 }
		 Else {
			 $json = $response.Content
		 }
		 
		 $content = $json | ConvertFrom-Json
		 $content.Answer
	 }
 }

@masroore
Copy link

CF, Goog & Quad9 all queries are working

@matt40k
Copy link
Author

matt40k commented Aug 31, 2022

Cheers @corey-alford, you ok if I use that and add it to the PSGallery?

@corey-alford
Copy link

Hey sorry missed the notification for this, yeah for sure. Would be cool if you could give me a credit on it or something but all good if too hard haha.

@corey-alford
Copy link

Hey @matt40k

I have also updated it as well so now you can use -type and it'll resolve those records when using Google or CloudFlare, updated version is below:

Resolve-DnsSSL -Domains <Domains Array> -Type <DNS Record Type> -Provider <CloudFlare, Google, Quad9>

Function Resolve-DnsSSL{
 param (
	[Parameter(Mandatory=$True)]
	[array]$domains,

	[Parameter()]
	[array]$Type = 'A',
 
	[Parameter()]
	[ValidateSet("CloudFlare","Google","Quad9")]
	[String]$Provider = "CloudFlare"
 )

	Foreach ($domain in $domains) {
	
		switch ($Provider) {
			"CloudFlare" {$url = 'https://cloudflare-dns.com/dns-query?name='+$domain+'&type='+$type; break}
			"Google"     {$url = 'https://dns.google.com/resolve?name=' + $domain + '&type='+$type; break}
			"Quad9"      {$url = 'https://dns.quad9.net:5053/dns-query?name=' + $domain; break}
		}
	
	
	
		$header = @{"accept"="application/dns-json"}
		$response = (Invoke-WebRequest -Uri $url -Headers $header -UseBasicParsing)
	
		if ($response.Content.GetType().name -eq "Byte[]") {
			$json = [System.Text.Encoding]::UTF8.GetString($response.Content)
		}
		Else {
			$json = $response.Content
		}
	
		$content = $json | ConvertFrom-Json
		$content.Answer
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment