PoshCode Archive  Artifact [5c9c29dee8]

Artifact 5c9c29dee855056c47cb79d5eff2f82401d5a23f3c8b5e8ab2e36709567856df:

  • File Get-MX.ps1 — part of check-in [3d55397e9b] at 2018-06-10 14:00:56 on branch trunk — Returns the priority mail server (SMTP) to send email directly to the SMTP server of a particular domain/email address. Uses NetCmdlets (get-dns). (user: Lance Robinson size: 1146)

# encoding: ascii
# api: powershell
# title: Get-MX
# description: Returns the priority mail server (SMTP) to send email directly to the SMTP server of a particular domain/email address.  Uses NetCmdlets (get-dns).
# version: 0.1
# type: function
# author: Lance Robinson
# license: CC0
# function: Get-MX
# x-poshcode-id: 5764
# x-derived-from-id: 5765
# x-archived: 2015-03-23T09:50:08
# x-published: 2015-03-02T21:53:00
#
#
#Returns the priority mail server (SMTP) for a particular email address.
function Get-MX {
param([string] $domain = $( Throw "Query required in the format domain.com or email@domain.com.") )
	
	#rip domain out of full email address if necessary:
	if ($domain.IndexOf("@")) { $domain = $domain.SubString($domain.IndexOf("@")+1) } 
	  	
  #get all the MX records for this domain, sorted by descending preference 
  $mxrecords = @(get-dns $domain -type MX | sort-object -Property PREFERENCE -des)
  
  #verify that there are some records
  if ($mxrecords.Length -eq 0) { Throw "No records found." }
  	
  #The correct record is the one with the lowest preference:
  $mxrecords[0].EXCHANGE
}