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
}
@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