Quantcast
Channel: Practical 365
Viewing all articles
Browse latest Browse all 546

Use PowerShell to Quickly Check Multiple MX Records

$
0
0

Most of the customers I work with have just a small number of domain names used for email in their organization. But every now and then I do some work with a customer who has a very large number of email domains.

Often I’m interested in checking the MX records configured for all of those domain names. One example is during an Office 365 migration, when all of the MX records are planned to be moved from their on-premises infrastructure to Exchange Online Protection.

Querying all of the MX records manually would be a long and tedious task. And it would be very boring to have to manually repeat the task at different stages of the migration project.

Fortunately we can query MX records using the Resolve-DnsName cmdlet in PowerShell. Here’s an example:

[PS] C:\>Resolve-DnsName -Name microsoft.com -Type MX | ft -auto
Name          Type TTL Section NameExchange                              Preference
----          ---- --- ------- ------------                              ----------
microsoft.com MX   10  Answer  microsoft-com.mail.protection.outlook.com 10

Here’s an example of a domain name with multiple MX records:

[PS] C:\>Resolve-DnsName -Name qantas.com.au -Type MX
Name          Type TTL Section NameExchange                   Preference
----          ---- --- ------- ------------                   ----------
qantas.com.au MX   598 Answer  cluster-g.mailcontrol.com      10
qantas.com.au MX   598 Answer  cluster-m.mailcontrol.com      10
qantas.com.au MX   598 Answer  cust20986-1.in.mailcontrol.com 20
qantas.com.au MX   598 Answer  cluster-k.mailcontrol.com      10
qantas.com.au MX   598 Answer  cust20986-3.in.mailcontrol.com 20
qantas.com.au MX   598 Answer  cust20986-2.in.mailcontrol.com 20

As you can see some domain names have one MX record, while others have several. So any script needs to handle multiple domain names *and* multiple MX records.

There’s a few ways we can handle querying multiple domain names. Basically we first need to capture the list of domain names into a variable. If the list of domain names we could do this with Get-Content:

[PS] C:\Scripts>$domains = @(Get-Content .\domains.txt)

If we want to get the list of domain names from the Exchange organization itself we can use Get-AcceptedDomain:

[PS] C:\Scripts>$domains = @((Get-AcceptedDomain).DomainName)

Now we simply pipe $domains into Resolve-DnsName and do a little filtering and sorting:

[PS] C:\Scripts>$domains | resolve-dnsname -Type MX -Server 8.8.8.8 | where {$_.QueryType -eq "MX"} | Select Name,NameExchange | Sort Name
Name                                                        NameExchange
----                                                        ------------
exchangeserverpro.mail.onmicrosoft.com                      exchangeserverpro-mail-onmicrosoft-com.mail.protection.o...
exchangeserverpro.net                                       maila.locklan.com.au
exchangeserverpro.onmicrosoft.com                           exchangeserverpro.mail.protection.outlook.com
office365bootcamp.net                                       office365bootcamp-net.mail.protection.outlook.com

In the example above I’ve used -Server and one of Google’s DNS server IP addresses to ensure I get the MX record from the public DNS zone and not any internal DNS zones I might be hosting for those domain names.

As you can see it is quite simple to query the MX records for multiple domain names by using PowerShell.


This article Use PowerShell to Quickly Check Multiple MX Records is © 2015 ExchangeServerPro.com

Get more Exchange Server tips at ExchangeServerPro.com

     

Viewing all articles
Browse latest Browse all 546

Trending Articles