I don’t talk about work very often here, mainly because there’s not much to talk about. Every day’s a mix of new challenges and the same old song & dance… and the dance usually involves backups, somehow.
Today, though, I dug in on a request to at least partially automate the task of retrieving and storing certain information about email domains in our client base. What better way to do that then with some PowerShell tinkering?
(Don’t answer that question. I know there are better ways, for various values of “better.” PS is the toolkit I need to use for work-specific reasons, let’s leave it at that, shall we? Play nice.)
Anyway. Here’s what I ended up with:
# Load in a few key variables right up top.
$Datestamp = Get-Date -format yyyyMMdd
$DisplayDate = Get-Date -format f
$DomainsList = Get-Content "E:\Projects\Scripting\DomainList.txt"
# Make sure the output directory exists before we get carried away.
$OutputFolder = "E:\Projects\Scripting\" + $Datestamp + "\"
if (!(Test-Path $OutputFolder)) {
New-Item $OutputFolder -ItemType Directory
}
# Now get to work!
ForEach ($Domain in $DomainsList) {
$OutFile = $OutputFolder + $Domain + ".txt"
echo $DisplayDate | Out-File $OutFile
echo "`nMX Record(s)" | Out-File -Append $OutFile
Resolve-DnsName -Type MX -Name $Domain | ? Type -eq MX | Select @{L="Host"; E={$_.NameExchange}}, Preference | Sort-Object Preference | Out-File -Append $OutFile
echo "`nSPF Record(s)" | Out-File -Append $OutFile
Resolve-DnsName -Name $Domain -Type TXT -Erroraction SilentlyContinue | ? {$_.Strings -match "v=spf1"} | Select -ExpandProperty Strings | Out-File -Append $OutFile
echo "`nDMARC Record(s) (if available)" | Out-File -Append $OutFile
Resolve-DnsName -Name "_dmarc.$Domain" -Type TXT -Erroraction SilentlyContinue | ? {$_.Strings -match "v=DMARC1"} | Select -ExpandProperty Strings | Out-File -Append $OutFile
# Introducing a 1-second pause so we aren't hammering DNS... just to be on the safe side.
Start-Sleep -Seconds 1
}
What is all of that? If it looks like nonsense to you, I’ll walk you through the key sections:
- That first block establishes some variables I’m going to use later on, such as the current date in just-numbers format (e.g. “20240516”) so I can make a directory just for today’s results. The text file with all the domains to check is listed there as well.
- We want to create the directory to contain today’s output files if it doesn’t already exist. (I might run this multiple times in a day due to glitches or adjustments.)
- Now we loop through that list of domains and for each line in the text file (which is just a domain, like ‘greyduck.net’ or ‘frell.co’) we create a new text file with today’s date at the top (in a more friendly format) followed by the results of a DNS check for MX, SPF, and DMARC records. At the end we pause for a second before looping to the next entry in the text file.
That’s it. It’s nothing terribly fancy, honestly. The result for each domain is a text file which looks a bit like this:
Thursday, May 16, 2024 2:18 PM
MX Record(s)
Host Preference
---- ----------
ASPMX.L.GOOGLE.COM 1
ALT1.ASPMX.L.GOOGLE.COM 5
ALT2.ASPMX.L.GOOGLE.COM 5
ASPMX2.GOOGLEMAIL.COM 10
ASPMX3.GOOGLEMAIL.COM 10
SPF Record(s)
v=spf1 include:_spf.google.com ~all
DMARC Record(s) (if available)
Don’t worry, I’m not giving out secrets. This information is available to literally anyone and any device that can perform a DNS lookup, as it’s necessarily public for the purpose of delivering email to my domain’s addresses.
Now, the scheduling and storage and upkeep for this little scripted solution is someone else’s problem (at the moment), so it’s time to down tools and move on to the next bit of fun…