SearchDNS.psm1
<#
.SYNOPSIS Inspired by Justin Wyllys' "DNS Checker", this PowerShell Module is meant to replace several NS Lookups usually done through the Command Prompt. It also borrows elements from Nikolay Petkov's 'WHOIS PowerShell Function', publicly available at: https://gallery.technet.microsoft.com/WHOIS-PowerShell-Function-ed69fde6 .DESCRIPTION The Module's only available function is designated as 'Search-Dns'. Its usage scenarios can be demonstrated with 'Get-Help Search-Dns -Examples'. .NOTES Author: Luís Pinto <Lispint> Version: 1.160817 .PARAMETER DomainName Specify one or more domain names on which to run the DNS queries. .PARAMETER ClearDnsCache Flush the DNS Cache. .EXAMPLE Search-Dns -ClearDnsCache This statement is analogous to 'IPConfig /flushDNS'. .EXAMPLE Search-Dns -DomainName Microsoft.com This statement will return all DNS records for 'Microsoft.com', querying the default DNS server. .EXAMPLE Search-Dns -DomainName Microsoft.com -CustomDnsServer This statement will ask the user for a custom DNS server, and return all its DNS records for 'Microsoft.com'. #> Function Search-Dns { Param ( [Parameter(Mandatory = $True, Position = 1)] [String[]]$DomainName, [Switch]$SIP, [Switch]$CNAME, [Switch]$MX, [Switch]$TXT, [Switch]$A, [Switch]$WHOIS, [Switch]$ClearDnsCache, [Switch]$CustomDnsServer ) #Clear DNS Cache Switch ($ClearDnsCache) { ($True) { Clear-DnsClientCache ; Write-Host "DNS Cache Cleared." -Fore Green ; "`n" }} #Custom DNS Server Switch ($CustomDnsServer) { ($True) { [String]$DnsServer = Read-Host "DNS Server" ; }} #Default Behavior: Query All Records If ( (-Not $SIP) -and (-Not $CNAME) -and (-Not $MX) -and (-Not $TXT) -and (-Not $A) -and (-Not $WHOIS) -and (-Not $ClearDnsCache) -and (-Not $CustomDnsServer) ) { $SIP = $CNAME = $MX = $TXT = $A = $True } #33 = SRV | 'Server Selection' Switch ($SIP) { ($True) { #Custom Table Formatting $Table = ` @{Expression = {$_.Type}; Label = "Type"; Width = 5}, ` @{Expression = {$_.Name}; Label = "Name"; Width = 45}, ` @{Expression = {$_.NameTarget}; Label = "Target"; Width = 40}, ` @{Expression = {$_.IP4Address}; Label = "IP Address"; Width = 15} If ($CustomDnsServer -Eq $False) { ForEach ($Dmn in $DomainName) { Write-Host ("SIP TLS `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 33 ("_sip._tls." + $Dmn) | FT $Table ; Write-Host ("SIP Federation `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 33 ("_sipfederationtls._tcp." + $Dmn) | FT $Table } } Else { ForEach ($Dmn in $DomainName) { Write-Host ("SIP TLS (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 33 ("_sip._tls." + $Dmn) -Server $DnsServer | FT $Table ; Write-Host ("SIP Federation (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 33 ("_sipfederationtls._tcp." + $Dmn) -Server $DnsServer | FT $Table } } }} #5 = CNAME | 'Canonical Name' Switch ($CNAME) { ($True) { #Custom Table Formatting $Table = ` @{Expression = {$_.Type}; Label = "Type"; Width = 5}, ` @{Expression = {$_.Name}; Label = "Name"; Width = 45}, ` @{Expression = {$_.NameHost}; Label = "Canonical Name"; Width = 50} If ($CustomDnsServer -Eq $False) { ForEach ($Dmn in $DomainName) { Write-Host ("LyncDiscover CNAME `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 5 ("lyncdiscover." + $Dmn) | FT $Table ; Write-Host ("AutoDiscover CNAME `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 5 ("autodiscover." + $Dmn) | FT $Table ; Write-Host ("MSOID CNAME `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 5 ("msoid." + $Dmn) | FT $Table } } Else { ForEach ($Dmn in $DomainName) { Write-Host ("LyncDiscover CNAME (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 5 ("lyncdiscover." + $Dmn) -Server $DnsServer | FT $Table ; Write-Host ("AutoDiscover CNAME (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 5 ("autodiscover." + $Dmn) -Server $DnsServer | FT $Table ; Write-Host ("MSOID CNAME (against $DnsServer) `n Domain Name: '$Dmn'")-Fore Green ; Resolve-DnsName -Type 5 ("msoid." + $Dmn) -Server $DnsServer | FT $Table } } }} #15 = MX | 'Mail Routing' Switch ($MX) { ($True) { #Custom Table Formatting $Table = ` @{Expression = {$_.Type}; Label = "Type"; Width = 5}, ` @{Expression = {$_.Preference}; Label = "Priority"; Width = 8}, ` @{Expression = {$_.NameExchange}; Label = "Hostname"; Width = 36}, ` @{Expression = {$_.IP4Address}; Label = "IP Address"; Width = 15} If ($CustomDnsServer -Eq $False) { ForEach ($Dmn in $DomainName) { Write-Host ("MX Record `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 15 $Dmn | FT $Table } } Else { ForEach ($Dmn in $DomainName) { Write-Host ("MX Record (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 15 $Dmn -Server $DnsServer | FT $Table } } }} #16 = TXT | 'Text Strings' Switch ($TXT) { ($True) { #Custom Table Formatting $Table = ` @{Expression = {$_.Type}; Label = "Type"; Width = 5}, ` @{Expression = {$_.Strings}; Label = "Value"; Width = 100} If ($CustomDnsServer -Eq $False) { ForEach ($Dmn in $DomainName) { Write-Host ("TXT `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 16 $Dmn | FT $Table } } Else { ForEach ($Dmn in $DomainName) { Write-Host ("TXT (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 16 $Dmn -Server $DnsServer | FT $Table } } }} #1 = A | 'IPv4 Server Address' Switch ($A) { ($True) { #Custom Table Formatting $Table = ` @{Expression = {$_.Type}; Label = "Type"; Width = 5}, ` @{Expression = {$_.Name}; Label = "Name"; Width = 45}, ` @{Expression = {$_.IPAddress}; Label = "IP Address"; Width = 15} If ($CustomDnsServer -Eq $False) { ForEach ($Dmn in $DomainName) { Write-Host ("Root A Record `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 1 $Dmn | FT $Table Write-Host ("Autodiscover A Record `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 1 ("autodiscover." + $Dmn) | FT $Table } } Else { ForEach ($Dmn in $DomainName) { Write-Host ("Root A Record (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 1 $Dmn -Server $DnsServer | FT $Table Write-Host ("Autodiscover A Record (against $DnsServer) `n Domain Name: '$Dmn'") -Fore Green ; Resolve-DnsName -Type 1 ("autodiscover." + $Dmn) -Server $DnsServer | FT $Table } } }} #Gather WHOIS Switch ($WHOIS) { ($True) { ForEach ($Dmn in $DomainName) { Write-Host ("WHOIS `n Domain Name: '$Dmn'") -Fore Green ; $Json = Invoke-WebRequest -Uri ("http://api.bulkwhoisapi.com/whoisAPI.php?domain=" + $Dmn + "&type=whois&token=7d3f08b98ab9f69ae15060a5b58ef1ee") | ConvertFrom-Json $Json.formatted_data | FL RegistrarURL, RegistrantName } }} } #End Function 'Search-Dns' |