all Technical posts

Tips and tricks for BizTalk monitoring with PowerShell

Tips and tricks for BizTalk monitoring with PowerShell. In this blog post I explain how you can use PowerShell to monitor a frequently failing receive location in BizTalk 2006.

I recently worked on a project that used a custom 3rd party adapter to retrieve messages from a B2B partner. The connection between both systems was very sensitive. The connection was lost every week during the weekend due the reboot of the server of the external party. After a while we saw that BizTalk was not able to reconnect and reestablish a connection. A monitoring system like SCOM could help us to alert and notify us of these problems. Although, this more or less means a human action was still required to fix the problem.

A simple PowerShell script should also solve this problem, maybe even quicker than the BizTalk operator could detect.
If you are working on BizTalk 2010 or 2013, there is a great open source library available that will increase the productivity for developing PowerShell scripts. You can find more information about this project here:

The project was running on a BizTalk 2006 environment with Windows 2003R2. I’ll highlight some interesting points I tackled during the development and setup.


The purpose of the script is to check a specific receive location every 30 minutes. If this receive location is disabled, we want to restart the host and enable the receive location. After 5 minutes we do an evaluation and, if necessary, do this again. If this process fails after 5 times, we want to send an email to an operator to check the connection manually and perform a manual action.


Install PowerShell

PowerShell 2.0 is supported for Windows server since version 2003R2, although, it might be possible that you need to update or explicitly install PowerShell. Installation details can be found here:

Configure PowerShell

By default, the execution of custom PowerShell scripts is disabled. If you would try to execute a script, following error would pop-up:

“… cannot be loaded because the execution of scripts is disabled in this system. Please see get-help about_signing for more details”


Luckily, we can disable this security feature with the Set-Execution command.
Open PowerShell with Administrator rights and execute following command:

Set-ExecutionPolicy Unrestricted

Note that this is the most unsafe configuration scenario, mostly used for development environments.


Script development

PowerShell offers a small IDE interface to help script developing. The IDE can be found in Start -> All Programs -> Accessoires -> Windows PowerShell -> Windows PowerShell IDE.

We’ll use the BizTalk.ExplorerOM and WMI to communicate to the BizTalk environment.

# Import external assembly and create a new object
[void] [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.BizTalk.ExplorerOM”)
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer

#BizTalk Config
$Catalog.ConnectionString = “SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI” #connectionstring to the mgmt db
$hostname = “xx” #hostname for the JMS host
$rcvLocation = “xx” #receive location

#Email Config
$emailFrom = “xx”
$emailTo = “xx”
$emailSubject = “!!! Connection lost to xxx”
$emailServer =”xx”

To make things more manageable, I made some helper functions:

#Function to retreive the status of the specific receive location
function getStatus(){
foreach ($receivePort in $catalog.ReceivePorts)
foreach($receiveLoc in $receivePort.ReceiveLocations | Where {$_.Name -eq $rcvLocation}){
return $receiveLoc.Enable

#Function to enable the receive location
function enableReceiveLocation(){
$location = get-wmiobject msbts_receivelocation -Namespace ‘rootMicrosoftBizTalkServer’ -Filter “name=’${rcvLocation}'”


What’s important here is that we perform a hard refresh of the BizTalk catalog after we try to enable the receive location. Otherwise, if we would read the old value again.

#Function to sends an error email
function sendEmail(){
$message = “An error occurred in the BizTalk flow. The connection towards the customer xx is lost and cannot be restarted after multiple tries.
Please connect to the environment and fix this problem manually. ”
$smtp=new-object Net.Mail.SmtpClient($emailServer)
$smtp.Send($emailFrom, $emailTo, $emailSubject, $message)


At the end of the file, I connect everything together:

$keeplooping = $true
$isok = $false
$i = 0;

#check status 5 times
while($keeplooping -eq $true){
Write-Host “Checking count: ${i}”
$isEnabled = getStatus
if($isEnabled -eq $false){
Write-Host “Receive location is not enabled, trying to restart the host and enabling the receive location”
#Restart host
Restart-Service -Displayname “BizTalk Service BizTalk Group : ${hostname}”
#wait a few seconds
Start-Sleep -s 10
#Enable receive location
#Wait 5 minutes
Start-Sleep -s 300
if($i -gt 4){
$keeplooping = $false
Write-Host “Receive location is enabled”
$keeplooping = $false
$isok = $true

if($isok -eq $false){
#send mail, we tried to restart host multiple times without success!
Write-Host “Sending mail”

Write-Host “Done”


What’s important is that you declare custom functions in the top of the script, since PowerShell reads from the beginning to the end.

I also saw that it is sometimes required to wait some seconds after a certain action. 
I retrieved error messages when I wanted to restart the receive location, since the host is busy restarting. This can be easily solved when you add a simple sleep command of a few seconds.

Schedule tasks

Task scheduling in Windows can be version specific, but this gives you an idea. I configured the interval to execute every 30 minutes and used this command:

PowerShell -file “c:file.ps1”

It might be worth it to manually trigger the schedule first and see if this works. If an error occurs, you probably won’t have enough time to read them, since the output window disappears fast. To troubleshoot this, change your command to this:

PowerShell -noexit -file “c:file.ps1”


We learnt how to get started with PowerShell, check the state of receive location, restart a host, send an email and configure a PowerShell scheduled task.

I am sure that my code can be optimized in many ways, but the main purpose is to give the idea and some guidelines.

At the end, I am happy that we accomplished a fast and easy solution where no operator should be involved and will work for 99% of the cases.

Enjoy scripting!

Subscribe to our RSS feed

Thanks, we've sent the link to your inbox

Invalid email address


Your download should start shortly!

Stay in Touch - Subscribe to Our Newsletter

Keep up to date with industry trends, events and the latest customer stories

Invalid email address


Great you’re on the list!