-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTest-DNSRecords.ps1
More file actions
193 lines (146 loc) · 7.26 KB
/
Copy pathTest-DNSRecords.ps1
File metadata and controls
193 lines (146 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
param(
[string]$DomainCsvFile = '.\Domains.txt',
[switch]$SendMail,
[switch]$ExportToCsv,
[switch]$TestExoDkim,
[switch]$ResolveSpfIncludes,
$DNSServer = '8.8.8.8'
)
# https://powershellisfun.com/2022/06/19/retrieve-email-dns-records-using-powershell/
$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path
$Delimiter = ';'
# Your SMTP server relay information
$SmtpServer = "smtp.yourserver.com"
$SmtpPort = 25
$To = "recipient@domain.com"
$From = "yourEmail@domain.com"
$MessageSubject = "DNS Mail Records Report"
function Export-TestResultToCsv {
param(
$ResultSetToExport
)
$FilePath = Join-Path -Path $ScriptDir -ChildPath ('DNSTestResult-{0}.csv' -f (Get-Date).ToString('yyyyMMdd') )
Write-Output ('Exporting results to {0}' -f $FilePath)
$ResultSet | Export-Csv -Path $FilePath -Delimiter $Delimiter -Encoding utf8 -Force -Confirm:$false
return $FilePath
}
if (Test-Path -Path $DomainCsvFile) {
$Domains = Import-Csv -Path $DomainCsvFile
}
if ($null -ne $Domains) {
Write-Output ('Using DNS {0} to resolve {1} domain(s)' -f $DNSServer, ($Domains | Measure-Object).Count)
$ResultSet = @()
Write-Output ('')
foreach ($DomainEntry in $Domains) {
#Check if domain name is valid, output warning it not and continue to the next domain (if any)
try {
$domain = $DomainEntry.Domain
Write-Output ('Resolving: {0}' -f $domain)
Resolve-DnsName -Name $domain -Server $DNSserver -ErrorAction Stop | Out-Null
# Test AutoDiscover Ad recrod
$AutoDiscoverARecord = (Resolve-DnsName -Name ('autodiscover.{0}' -f$domain) -Type A -Server $DNSserver -ErrorAction SilentlyContinue).IPAddress
$AutoDiscoverCNAMERecord = (Resolve-DnsName -Name ('autodiscover.{0}' -f$domain) -Type CNAME -Server $DNSserver -ErrorAction SilentlyContinue).NameHost
# DKIM
$DKIM1Record = Resolve-DnsName -Name ('selector1._domainkey.{0}' -f $domain) -Type CNAME -Server $DNSserver -ErrorAction SilentlyContinue
$DKIM2Record = Resolve-DnsName -Name ('selector1._domainkey.{0}' -f $domain) -Type CNAME -Server $DNSserver -ErrorAction SilentlyContinue
# DMARCRecord
$DMARCRecord = (Resolve-DnsName -Name ('_dmarc.{0}' -f $domain) -Type TXT -Server $DNSserver -ErrorAction SilentlyContinue | Where-Object Strings -Match 'DMARC').Strings
# MTA-STS
$MTASTSRecord = (Resolve-DnsName -Name ('_mta-sts.{0}' -f $domain) -Type TXT -Server $DNSserver -ErrorAction SilentlyContinue | Where-Object Strings -Match 'v=sts').Strings
#MX
$MXRecord = (Resolve-DnsName -Name $domain -Type MX -Server $DNSserver -ErrorAction SilentlyContinue).NameExchange
# SPF
$SPFRecord = (Resolve-DnsName -Name $domain -Type TXT -Server $DNSserver -ErrorAction SilentlyContinue | Where-Object Strings -Match 'v=spf').Strings
$includes = (Resolve-DnsName -Name $domain -Type TXT -Server $DNSserver -ErrorAction SilentlyContinue | Where-Object Strings -Match 'v=spf').Strings -split ' ' | Select-String 'Include:'
#Set variables to Not enabled or found if they can't be retrieved
$ErrorText = 'Not configured'
if ($null -eq $DKIM1Record -and $null -eq $DKIM2Record) {
$dkim = ('{0} or using custom DKIM hostname' -f $ErrorText)
}
else {
$dkim = "$($DKIM1Record.Name) , $($DKIM2Record.Name)"
}
if ($null -eq $MTASTSRecord) {
$MTASTSRecord = $ErrorText
}
else {
# Fetch MTA-STS text file
$MTASTSContent = Invoke-WebRequest -Uri ('https://mta-sts.{0}/.well-known/mta-sts.txt' -f $domain) -ErrorAction SilentlyContinue
if($null -ne $MTASTSContent) {
$MTASTSTextFile = $MTASTSContent.Content.Replace([System.Environment]::NewLine,', ')
}
else {
$MTASTSTextFile = $ErrorText
}
}
if ($null -eq $DMARCRecord) {
$DMARCRecord = $ErrorText
}
if ($null -eq $MXRecord) {
$MXRecord = $ErrorText
}
if ($null -eq $SPFRecord) {
$SPFRecord = $ErrorText
}
if ($null -eq $AutoDiscoverCNAMERecord) {
$AutoDiscoverCNAMERecord = $ErrorText
}
if (($AutoDiscoverARecord).count -gt 1 -or $null -ne $AutoDiscoverCNAMERecord) {
$AutoDiscoverARecord = $ErrorText
}
if ($null -eq $includes) {
$includes = $ErrorText
}
else {
$foundincludes = foreach ($include in $includes) {
if ((Resolve-DnsName -Server $DNSserver -Name $include.ToString().Split(':')[1] -Type txt -ErrorAction SilentlyContinue).Strings) {
[PSCustomObject]@{
SPFIncludes = "$($include.ToString().Split(':')[1]) : " + $(Resolve-DnsName -Server $DNSserver -Name $include.ToString().Split(':')[1] -Type txt).Strings
}
}
else {
[PSCustomObject]@{
SPFIncludes = $ErrorText
}
}
}
}
$ResultSet += [PSCustomObject]@{
'Domain Name' = $domain
'Autodiscover IP-Address' = $AutoDiscoverARecord
'Autodiscover CNAME ' = $AutoDiscoverCNAMERecord
'DKIM Record' = $dkim
'DMARC Record' = "$($DMARCRecord)"
'MTA-STS Record' = "$($MTASTSRecord)"
'MTA-STS File Content' = $MTASTSTextFile
'MX Record(s)' = $MXRecord -join ', '
'SPF Record' = "$($SPFRecord)"
#'SPF Include values' = "$($foundincludes.SPFIncludes)" -replace "all", "all`n`b"
'SPF Include values' = "$($foundincludes.SPFIncludes)" #-replace "all", "all"
}
}
catch{}
}
if ($ExportToCsv) {
# Simply export to a CSV file in the script directory
Export-TestResultToCsv -ResultSetToExport $ResultSet
}
if ($SendMail) {
# Export results to CSV and fetch file path for attachment
$CsvFilePath = Export-TestResultToCsv -ResultSetToExport $ResultSet
# Send email
$emailBody = $ResultSet | ConvertTo-Html | Out-String
$smtpServer = "smtp.yourserver.com"
$smtpPort = 587
$smtpUser = "yourEmail@domain.com"
$smtpPass = "yourPassword"
$to = "recipient@domain.com"
$from = "yourEmail@domain.com"
$subject = "DNS Mail Records Report"
$credentials = New-Object System.Management.Automation.PSCredential ($smtpUser, (ConvertTo-SecureString $smtpPass -AsPlainText -Force))
Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl -Credential $credentials -To $to -From $from -Subject $subject -Body $emailBody -BodyAsHtml -Attachments $CsvFilePath
}
else {
Write-Output $ResultSet
}
}