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:
[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.Operations")
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$Catalog.ConnectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"
$rcvLocation = ""
$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 sendEmail($message)
{
$smtp=new-object Net.Mail.SmtpClient($emailServer)
$smtp.Send($emailFrom, $emailTo, $emailSubject, $message)
}
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"})
{
$DisabledSendPorts = $DisabledSendPorts + [Environment]::NewLine + $sendPort.Name
}
return $DisabledSendPorts
}
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
$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:
The mail contents are as follows:
Points of Interest
The powershell script can be scheduled as service to monitor and send mails in a periodic fashion.