Created
August 4, 2023 14:41
-
-
Save changbowen/2923362cfb8032d7d61654746a94b549 to your computer and use it in GitHub Desktop.
DNS query PowerShell script that saves you from typing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string]$Address, | |
[string]$Server | |
) | |
function FormatDnsRecords { | |
[CmdletBinding()] | |
param( | |
[parameter(ValueFromPipeline, Mandatory)] | |
[Microsoft.DnsClient.Commands.DnsRecord]$Record | |
) | |
process { | |
return [pscustomobject]@{ | |
Name = $Record.Name | |
Type = $Record.Type | |
Section = $Record.Section | |
# QueryType = $Record.QueryType | |
Record = switch ($Record) { | |
{ | |
$_ -is [Microsoft.DnsClient.Commands.DnsRecord_A] -or $_ -is [Microsoft.DnsClient.Commands.DnsRecord_AAAA] | |
} { $_.IPAddress } | |
{ | |
$_ -is [Microsoft.DnsClient.Commands.DnsRecord_PTR] | |
} { $_.NameHost } | |
} | |
} | |
} | |
} | |
function ResolveAddress { | |
param ( | |
[string]$Address, | |
[string]$Server | |
) | |
$params = @{ Name = $Address } | |
if ($Server) { $params.Server = $Server } | |
Write-Host 'A / AAAA:' -ForegroundColor Cyan | |
$records = @(Resolve-DnsName -ErrorAction Ignore @params) | |
if (!$records) { | |
Write-Warning "No records returned from $(if ($Server) { $Server } else { "default server" })." | |
} else { | |
$records | FormatDnsRecords | Format-Table -AutoSize | |
} | |
Write-Host | |
Write-Host 'ANY:' -ForegroundColor Cyan | |
$records = @(Resolve-DnsName -Type ANY -ErrorAction Ignore @params) | |
if (!$records) { | |
Write-Warning "No records returned from $(if ($Server) { $Server } else { "default server" })." | |
} else { | |
$records | FormatDnsRecords | Format-Table -AutoSize | |
} | |
Write-Host | |
} | |
if (!$Address) { | |
while ($true) { | |
$Address = Read-Host -Prompt "Address$(if ($global:lastAddress) { " [$($global:lastAddress)]" })" | |
if ($Address) { $global:lastAddress = $Address } else { $Address = $global:lastAddress } | |
$Server = Read-Host -Prompt "Server$(if ($global:lastServer) { ' ("." for default)' + " [$($global:lastServer)]" })" | |
if ($Server -eq '.') { $Server = $null; $global:lastServer = $null } | |
elseif ($Server) { $global:lastServer = $Server } else { $Server = $global:lastServer } | |
if ($Address) { | |
Clear-Host | |
ResolveAddress -Address $Address -Server $Server | |
Write-Host | |
} | |
} | |
} | |
ResolveAddress -Address $Address -Server $Server | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment