BizTalk DevOps: Monitor your BizTalk environment using PowerShell –Monitoring Ports (Stopped/disabled/unelisted) with Auto-Healing capabilities

Posted: February 15, 2016 in BizTalk, PowerShell
Tags: , , , , , , ,

In the sequence of my last post, welcome back, once again, to this serial of articles about Monitor your BizTalk environment using PowerShell.

Today we will talk about a topic that was also previously covered by Jeroen, ” Monitor your BizTalk environment using PowerShell – Port monitoring”, but this time, I will also add Auto Healing functionalities to the script.

Note: This type of script must be viewed as a complement to the tools mentioned above or used in the absence of them.

One of the principal needs for BizTalk Administrators is the ability to monitor the health of BizTalk environments and react promptly to possible problems, you can accomplish this by using certain tools such as: BizTalk Administration Console; BizTalk360; SCOM and many more… However, unfortunately many times, some of these tools are not available for us but we still need to accomplish this task.

There are bunch of things that can go wrong and the longer they have in an error/failure situation, more impact could have in your business! So, we don’t just want to be notified of failures (that will always lead to a manual human intervention) but instead, when possible, be also able to try to automatically fix it, bringing them to the desired state (running, resumed, enable – depending on the artifact)

So how can PowerShell help us?

With this script you can be able to monitoring your BizTalk environment for disabled receive locations and stopped or unenlisted send ports, and automatically resume then according to certain conditions, using PowerShell.

A mail notification will be sent only if this scripts finds any receive location or send port in a non-conform situation.

This sample will demonstrate how can you easily create a script with a set of conditions to be able to monitor and to automatically fix the receive locations and send ports to the desired state:

  • “Enable” for receive locations
  • “Started” for send ports

This script allows you to set:

  • Set your email notification settings
#Set mail variables
[STRING]$PSEmailServer = "mySMTPServer" #SMTP Server.
[STRING]$SubjectPrefix = "Port status - "
[STRING]$From = "biztalksupport@mail.pt"
[array]$EmailTo = ("sandro.pereira@devscope.net")

The following Windows PowerShell script is a fragment that will help us demonstrate this capabilities:

#Get Counters: Check if there are any disable receive locations and stopped/unelisted send ports
if ($sendports.count -ne (get-wmiobject MSBTS_SendPort -namespace 'root\MicrosoftBizTalkServer' -filter '(Status = 3)').count)
{
    [BOOL]$script:Continuescript = $True
    [INT]$countSendPorts = $sendports.count - $(get-wmiobject MSBTS_SendPort -namespace 'root\MicrosoftBizTalkServer' -filter {Status = 3}).count
}
if ($ReceiveLocations.count -ne (get-wmiobject MSBTS_ReceiveLocation -namespace 'root\MicrosoftBizTalkServer' -filter '(IsDisabled = False)').count)
{
    [BOOL]$script:Continuescript = $True
    [INT]$countReceivePorts = $ReceiveLocations.count - $(get-wmiobject MSBTS_ReceiveLocation -namespace 'root\MicrosoftBizTalkServer' -filter '(IsDisabled = False)').count
}

if($countSendPorts -gt 0)
{
    $mailTextReportPT += "" + $countSendPorts + " send ports stopped; "
    $mailBodyPT += "<h3>Send ports Stopped</h3>"

    #Add mail content for stopped send ports
    Foreach ($SendPort in $SendPorts)
    {
        #only add stopped send ports to the mail
        if ($SendPort.status -eq 2 -OR $SendPort.status -eq 1)
        {
            $StoppedSendPort = $True
            #Set status to a user friendly name
            if ($SendPort.status -eq 2)
            {
                $SendPortStatus = "Stopped"
            }
            elseif ($SendPort.status -eq 1)
            {
                $SendPortStatus = "Unelisted"
            }
    
            #Add mail content
            $mailBodyPT += "<th><b><font color='red'>" + $SendPort.name + "</font></b></th>"
            $mailBodyPT += "<table style='boder:0px 0px 0px 0px;'>"   
     
            $mailBodyPT += "<TR style='background-color:white;'><TD>Status</TD>"
            $mailBodyPT += "<TD><b><font color='red'>" + $SendPortStatus + "</font><b></TD></TR>"
        
            $mailBodyPT += "<TR style='background-color:rgb(245,245,245);';><TD>URI</TD>"
            $mailBodyPT += "<TD>" + $SendPort.PTAddress + "</TD></TR>"
        
            $mailBodyPT += "<TR style='background-color:white;'><TD>Transport type</TD>"
            $mailBodyPT += "<TD>" + $SendPort.PTTransportType + "</TD></TR>"

            if ($SendPort.status -eq 2)
            {
                #Auto-Healing feature
                $SendPort.InvokeMethod("Start",$null)

                $mailBodyPT += "<TR style='background-color:rgb(245,245,245);';><TD><font color='green'>Note</font></TD>"
                $mailBodyPT += "<TD><font color='green'>The Stopped port was automatically Started by this script! Please access the environment to check if ports are running correctly.</font></TD></TR>"
            }

            $mailBodyPT += "</table>"
            $mailBodyPT += "<BR><BR>"
        }
    }
}

if($countReceivePorts -gt 0)
{
    $mailTextReportPT += "" + $countReceivePorts + " receive locations disabled; "
    $mailBodyPT += "<h3>Receive locations Disabled</h3>"

    #Add mail content for stopped receive locations
    Foreach ($ReceiveLocation in $ReceiveLocations)
    {
        #only add stopped receive locations to the mail
        if ($ReceiveLocation.IsDisabled -eq "True")
        {
            $StoppedReceiveLocation = $True
            
            #Set status to a user friendly name
            $ReceiveLocationStatus = "Disabled"
        
            #Add mail content
            $mailBodyPT += "<th><b><font color='red'>" + $ReceiveLocation.name + "</font></b></th>"
            $mailBodyPT += "<table style='boder:0px 0px 0px 0px;'>"           
            $mailBodyPT += "<TR style='background-color:white;'><TD>Status</TD>"
            $mailBodyPT += "<TD><b><font color='red'>" + $ReceiveLocationStatus + "</font></b></TD></TR>"
            
            $mailBodyPT += "<TR style='background-color:rgb(245,245,245);';><TD>URI</TD>"
            $mailBodyPT += "<TD>" + $ReceiveLocation.InboundTransportURL + "</TD></TR>"
            
            $mailBodyPT += "<TR style='background-color:white;'><TD>Transport type</TD>"
            $mailBodyPT += "<TD>" + $ReceiveLocation.AdapterName + "</TD></TR>"

            #Auto-Healing feature
            $ReceiveLocation.InvokeMethod("Enable",$null)

            $mailBodyPT += "<TR style='background-color:rgb(245,245,245);';><TD><font color='green'>Note</font></TD>"
            $mailBodyPT += "<TD><font color='green'>The Disabled port was automatically Enable by this script! Please access the environment to check if ports are running correctly.</font></TD></TR>"

            $mailBodyPT += "</table>"
            $mailBodyPT += "<BR><BR>"
        }
    }
}

Note: The script should be adjusted to your needs. Maybe having a variable or a list of receive location and Send ports that should be excluded for the monitoring or have a different expected status.

Here is example expected report output from running the Windows PowerShell script sample, if any of the ports are in an unwanted state.

BizTalk-Ports-Status-Report

Again, this type of script must be viewed as a complement to the tools mentioned above or used in the absence of them.

THIS POWERSHELL IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

The script can be found and download on Microsoft TechNet Gallery:
Monitoring BizTalk Server Ports with Auto-Healing capabilities with PowerShell (14.0 KB)
Microsoft TechNet Gallery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s