NServiceBus replay error queue from remote server

Here is an approach for playing NServiceBus error queue from Remote server. Quite often this is required if the operation team does not have access to teh Messaging server or you have a Distributor setup. This setup uses references to ReturnToSourceQueue.exe tools that is supplied from NServiceBus.

Save the command below in a .ps1 file and run it from Remote server. Remeber you need to have permission to run PowerShell in local server and Read/Write permission to the Error qs in the remote server.

param (

[Parameter(Mandatory=$false, HelpMessage=‘-Computername – The Servername for the Powershell session.’)]

[string]$Computername=“YOURREMOTECOMPUTERNAME”,

[Parameter(Mandatory=$false, HelpMessage=‘-Errorqueuename – The name of the Error Queue to be replayed.’)]

[string]$Errorqueuename=“error”,

[Parameter(Mandatory=$false, HelpMessage=‘-Path – The path location to the ReturnToSourceQueue.exe.’)]

[string]$Locationpath=“C:\install\nServiceBus\tools”

 );

##### check the the server, error queue name and Path exists #####

Write-Host “Checking Server: “$Computername;

Write-Host “Checking Error Queue: “$Errorqueuename ;

Write-Host“Checking Path: “$Locationpath ;

##### creates a remote session on the Servercomputer #####

$sessiontem = new-pssession-computername$Computername;

##### change directory to the location of the ReturnToSourceQueue.exe #####

Invoke-Command -Session $sessiontem {cd“C:\install\nServiceBus\tools”};

##### invoke the command to call replay the error queue #####

Invoke-Command -Session $sessiontem {.\ReturnToSourceQueue.exe error all};

##### close pssession #####

Remove-pssession $sessiontem;

##### completed #####

Write-Host $Errorqueuename“queue replayed successfully.” ;