Soldato
- Joined
- 25 Mar 2004
- Posts
- 16,007
- Location
- Fareham
Hi all,
I'm in the middle of writing a PowerShell script that checks a list of domains and sees if the domain exists in DNS, whether it has a valid www.domain.com record, a valid MX record, and whether the website on the domain responds to HTTP requests.
I'm most of the way through it now, but I am wondering if I am doing this in the best way, or if there is a better way to achieve what I want to do.
Essentially I am leveraging nslookup to get the DNS records, but I then have to resort to parsing the values for text strings (which I don't like doing - unexpected results can throw it off). For the website checks I am utilising the System.Net.WebRequest .NET class to check the website responds which seems to work well.
Is there a better way to check the DNS records for all of these domains than throwing queries into nslookup? I find catching exceptions not very good in nslookup for exampe as it's not built into PowerShell.
This is my script so far, takes input domain list from C:\temp\domains.txt, all domains are in the format www.domain.com in the text file:
I'm in the middle of writing a PowerShell script that checks a list of domains and sees if the domain exists in DNS, whether it has a valid www.domain.com record, a valid MX record, and whether the website on the domain responds to HTTP requests.
I'm most of the way through it now, but I am wondering if I am doing this in the best way, or if there is a better way to achieve what I want to do.
Essentially I am leveraging nslookup to get the DNS records, but I then have to resort to parsing the values for text strings (which I don't like doing - unexpected results can throw it off). For the website checks I am utilising the System.Net.WebRequest .NET class to check the website responds which seems to work well.
Is there a better way to check the DNS records for all of these domains than throwing queries into nslookup? I find catching exceptions not very good in nslookup for exampe as it's not built into PowerShell.
This is my script so far, takes input domain list from C:\temp\domains.txt, all domains are in the format www.domain.com in the text file:
Code:
#Uses NSLookup to get DNS results based on parameters
Function Get-DNSRecords
{
param
(
$DomainName,
$RecordType,
$Server
)
$DNSRecordCommand = "nslookup -querytype=$RecordType -timeout=10 $DomainName $Server"
Write-Host -ForeGroundColor "Magenta" $DNSRecordCommand
$DNSRecords = Invoke-Expression $DNSRecordCommand
return $DNSRecords
}
#Parse WWW Records
Function Parse-WWWRecords
{
param
(
$DNSRecord
)
[array]$ARecords = $Null
for ($i = 0; $i -lt $DNSRecord.Count; $i++)
{
if ($DNSRecord[$i] -match "Name")
{
$ARecords += ($DNSRecord[($i + 1)]).Split(" ")[-1]
}
}
if ($ARecords -ne $Null)
{
return $ARecords -Join " ; "
}
else
{
return $Null
}
}
#Parse NS Records
Function Parse-NSRecords
{
param
(
$DNSRecord
)
[array]$NSRecords = $Null
for ($i = 0; $i -lt $DNSRecord.Count; $i++)
{
if ($DNSRecord[$i] -match "nameserver")
{
$NSRecords += ($DNSRecord[($i)]).Split(" = ")[-1]
}
}
if ($NSRecords -ne $Null)
{
return $NSRecords -Join " ; "
}
else
{
return $Null
}
}
#Parse MX Records
Function Parse-MXRecords
{
param
(
$DNSRecord
)
$DNSRecord = $DomainMX
[array]$MXRecords = $Null
for ($i = 0; $i -lt $DNSRecord.Count; $i++)
{
if ($DNSRecord[$i] -match "mail exchanger")
{
$MXRecords += ($DNSRecord[($i)]).Split(" = ")[-1]
}
}
if ($MXRecords -ne $Null)
{
return $MXRecords -Join " ; "
}
}
#Checks if Website exists
Function Get-HTTPResponse
{
param
(
$URL,
$TimeOut
)
Write-Host -ForeGroundColor "Magenta" $URL
$WebRequest = [System.Net.WebRequest]::Create($URL)
$WebRequest.TimeOut = $TimeOut
Try
{
#Attempt this
$WebResponse = $WebRequest.GetResponse()
}
Catch [Exception]
{
#Output Exception if any occurs
Write-Host $($_.Exception)
return ([string]$Error[0].FullyQualifiedErrorId + " - " + [string]$Error[0])
}
Finally
{
#Close connection if one has been opened
if ($WebResponse -ne $Null)
{
$WebResponse.Close()
}
}
if ($WebResponse.StatusCode -eq "OK")
{
Write-Host "Domain Reachable"
return $True
}
else
{
Write-Host "Domain Unreachable"
return $False
}
}
$MasterArray = @()
$Count = 0
$Server = "8.8.8.8"
[array]$Domains = gc "C:\temp\domains.txt"
foreach ($Domain in $Domains)
{
$Count++
Write-Host -ForeGroundColor "Yellow" "$Domain #$Count"
$TempArray = @()
$TempArray = "" | Select DomainName, PrimaryNS, MXRecord, WWWRecord, WebsiteResponds
[array]$DomainNS = Get-DNSRecords ($Domain.Replace("www.","") + ".") "NS" $Server
$DomainNS | Out-Host
[array]$DomainWWW = Get-DNSRecords ($Domain + ".") "A" $Server
$DomainWWW | Out-Host
[array]$DomainMX = Get-DNSRecords ($Domain.Replace("www.","") + ".") "MX" $Server
$DomainMX | Out-Host
$TempArray.DomainName = ($Domain.Replace("www.",""))
$TempArray.PrimaryNS = Parse-NSRecords $DomainNS
$TempArray.MXRecord = Parse-MXRecords $DomainMX
$TempArray.WWWRecord = Parse-WWWRecords $DomainWWW
#If domain has a WWW record then check if url responds to HTTP response
if ($TempArray.WWWRecord -ne $Null)
{
$TempArray.WebsiteResponds = Get-HTTPResponse "http://$Domain" 20000
}
$MasterArray += $TempArray
}
$MasterArray | ft