Monday, October 03, 2016

Gathering MX Records for Office 365 Domains

I have come across the need for a simple output of MX records for each domain in an Office 365.

An Office 365 tenant can contain multiple domains in a verified or unverified state.

This script simply outputs the MX records for each domain that has been verified into a CSV file named after the tenant ID.  The CSV file is output to the %temp% by default.

You will need to run the script inside a PowerShell session connected to Office 365.

#This script collects the MX records for all domains which have been verified in an O365 tenant.
#Run the script from within an O365 PowerShell session.
#Author: Ben Owens
#Date: 03/10/2016

$temp = $env:temp
$TenantID = Get-MSOLDomain | Where {$_.IsInitial -eq "TRUE"} | Select Name -ExpandProperty Name
$Domains = Get-MSOLDomain | Where {$_.Status -eq "Verified"} | Select Name
$MXRecords = ForEach ($Domain in $Domains) {Resolve-DnsName -Name $Domain.Name -Type MX | Where {$_.QueryType -eq "MX"}}
$MXRecords | Export-CSV "$Temp\$TenantID-mxrecords.csv" -NoTypeInformation
$Output = "$Temp\$TenantID-mxrecords.csv"
Notepad $Output

Example Output:


Post a Comment