Exchange 2013 Test-Mailflow Error for Remote Mailbox Servers

Following on from my last post about errors when running Test-ServiceHealth for Exchange 2013 Client Access servers, here is another PowerShell test cmdlet that behaves unexpectedly in Exchange 2013.

Test-Mailflow, as the name suggests, is used to test mail flow for Mailbox servers. The TechNet page provides the following example syntax:

Test-Mailflow Mailbox1 -TargetMailboxServer Mailbox2

What isn’t documented is the error that occurs if you try to run that command when your PowerShell session is connected to a server other than “Mailbox1″.

[PS] C:\scripts>Test-Mailflow E15MB1 -TargetMailboxServer E15MB2

[Microsoft.Mapi.MapiExceptionSendAsDenied]: MapiExceptionSendAsDenied:
Unable to submit message. (hr=0x80070005,ec=1244)

This error did not occur in Exchange 2010 under the same conditions.

As with the Test-ServiceHealth issues from my last post this situation is frustrating for me because I use Test-Mailflow in my Test-ExchangeServerHealth.ps1 script. So I needed to find a way to still perform the test while avoiding the error.

The solution was found by using PowerShell remoting. I’ve written this proof of concept code to demonstrate how remoting can be used to get a result for Test-Mailflow.

Note this is demo code only and is not intended to be a functional script. I will be incorporating this code into Test-ExchangeServerHealth.ps1 in the near future.

#Proof of concept code to test mail
#flow on remote E15 servers

[CmdletBinding()]
param (
	[Parameter( Mandatory=$false)]
	[string]$Server
)

Write-Verbose "Creating PSSession for $server"
try
{
    $url = (Get-PowerShellVirtualDirectory -Server $server | Where {$_.Name -eq "Powershell (Default Web Site)"}).InternalURL.AbsoluteUri
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP
}
catch
{
    Write-Verbose "Something went wrong"
    Write-Warning $_.Exception.Message
    EXIT
}
    

try
{
    Write-Verbose "Running mail flow test on $Server"
    $result = Invoke-Command -Session $session {Test-Mailflow} -ErrorAction STOP
    $testresult = $result.TestMailflowResult

}
catch
{
    Write-Verbose "An error occurred"
    Write-Warning $_.Exception.Message
    EXIT
}

Write-Host "Mail flow test: $testresult"

Write-Verbose "Removing PSSession"

Remove-PSSession $session.Id

Here is an example of the output.

[PS] C:\scripts>.\Test-E15MailFlow.ps1 E15MB1
Mail flow test: Success


[PS] C:\scripts>.\Test-E15MailFlow.ps1 E15MB2 -Verbose
VERBOSE: Creating PSSession for E15MB2
VERBOSE: Running mail flow test on E15MB2
Mail flow test: Success
VERBOSE: Removing PSSession
About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Find Paul on Twitter, LinkedIn or Google+, or get in touch for consulting/support engagements.

Comments

  1. Patrick Butler says:

    Intresting, so using this information I was able to configure mailflow in the Test-ExchangeServerHealth.ps1 by adding a variable $lserver = hostname and then in the mailflow adding/modifying
    $flow = Test-Mailflow $lserver -TargetMailboxServer $server -ErrorAction Stop

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.