Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / biztalk

Monitor BizTalk ports/suspended messages and send emails using Powershell script

5.00/5 (2 votes)
6 Dec 2015CPOL 11.4K   98  
This tip explains about a way using PowerShell to monitor all the disabled send ports/receive locations and suspended messages and send an email about these info

Introduction

This article explains about the approach using powershell script to monitor the send ports, receive locations and suspended messages in a BizTalk server and send a summarized email message about it to the users.  

Background

BizTalk Server, Windows PowerShell

Code Description

This PowerShell script refers to the 2 libraries: Microsoft.BizTalk.ExplorerOM and Microsoft.BizTalk.Operations.  

NOTE: In 64 bit machines, it may give error that Microsoft.BizTalk.ExplorerOM cannot be instantiated in 64 bit process and will only work in 32 bit process.  If so, use the PowerShell from this location: C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0

The script goes through the receive locations in BizTalk and finds the ones that are not enabled.  Also iterates through the send ports and find the ones that are not started.  Goes through the list of service instances and finds out the suspended messages.  The PowerShell script is given below:

PowerShell
# Import external assembly and create a new object
[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.Operations")
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer  
#BizTalk Config
$Catalog.ConnectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"  
#connectionstring to the mgmt db
$rcvLocation = "" 
#receive location  
#Email Config
$emailFrom = "krishna.seetharaman@aspiresys.com"
$emailTo = "krishna.seetharaman@aspiresys.com"
$emailSubject = "!!! Disabled Receive/Send Ports Suspended Messages " + $(Get-WmiObject Win32_Computersystem).name
$emailServer ="Give your SMTP Server Name"



#Function to sends an error email
function sendEmail($message)
{    
     $smtp=new-object Net.Mail.SmtpClient($emailServer)    
     $smtp.Send($emailFrom, $emailTo, $emailSubject, $message)
}


#Function to retreive the status of the specific receive location
function getDisabledReceiveLocations()
{   
	foreach ($receivePort in $catalog.ReceivePorts)   
	{     
	  foreach($receiveLoc in $receivePort.ReceiveLocations  | Where {$_.Enable -eq $false})
		{ 
		  $DisabledRcvLocations = $DisabledRcvLocations + [Environment]::NewLine + $receiveLoc.Name  
		}   
	}
  return $DisabledRcvLocations
}   

function getDisabledSendPorts()
{   
	foreach ($sendPort in $catalog.SendPorts | Where {$_.Status -ne "Started"})   
	{ 
		#Write-Host $sendPort.Name : $sendPort.Status  
		$DisabledSendPorts = $DisabledSendPorts + [Environment]::NewLine + $sendPort.Name       
	}
    return $DisabledSendPorts
}   

#Function to enable the receive location
function enableReceiveLocation()
{   
   $location = get-wmiobject msbts_receivelocation -Namespace 'root\MicrosoftBizTalkServer' -Filter "name='${rcvLocation}'" 
   $location.Enable()   
   $Catalog.Refresh()
}

function getSuspendedMessages()
{
	$bo = New-Object Microsoft.BizTalk.Operations.BizTalkOperations
	$serviceInstances = $bo.GetServiceInstances()
	foreach ($instance in $serviceInstances)
	{
	$msgApp = "Application :" + $instance.Application + [Environment]::NewLine
	$msgApp = $msgApp + "Service Name :" + $instance.ServiceType + [Environment]::NewLine + [Environment]::NewLine
		foreach ($mesg in $instance.Messages)
			{
			  $AppMsgs = "MessageID : " + $mesg.MessageID + [Environment]::NewLine
			  $AppMsgs = $AppMsgs + "Instance ID: " + $mesg.InstanceID + [Environment]::NewLine
			  $AppMsgs = $AppMsgs + "Instance Status :" + $mesg.InstanceStatus + [Environment]::NewLine
			  $AppMsgs = $AppMsgs + "Adapter Name :" + $mesg.AdapterName + [Environment]::NewLine
			  $AppMsgs = $AppMsgs + "Creation Time :" + $mesg.CreationTime + [Environment]::NewLine
			  $AppMsgs = $AppMsgs + "Error Description:" + $mesg.ErrorDescription + [Environment]::NewLine
			}
	$msgApp = $msgApp + $AppMsgs + [Environment]::NewLine			
	$FullMsg = $FullMsg + $msgApp
	}
	
	return $FullMsg;
}

$DisabledRcvLocations = getDisabledReceiveLocations
$message = "Host Name: " + $(Get-WmiObject Win32_Computersystem).name + [Environment]::NewLine
$message = $message + "The following Receive Locations are disabled : " + $DisabledRcvLocations

$DisabledSendPorts = getDisabledSendPorts
$message = $message + [Environment]::NewLine + [Environment]::NewLine + "The following Send Ports are disabled : " + 

$DisabledSendPorts
#echo $message
$SuspendedMessages = getSuspendedMessages
$SuspendedMessages = "Following are the Suspended Messages : " + [Environment]::NewLine + $SuspendedMessages
$message = $message + [Environment]::NewLine + [Environment]::NewLine + $SuspendedMessages
echo $message
sendEmail($message)

 

When we execute the script, we get the output as below:

Image 1

The mail contents are as follows:

Image 2

Points of Interest

The powershell script can be scheduled as service to monitor and send mails in a periodic fashion.  

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)