Friday 9 October 2015

Enabling BizTalk Receive locations using PowerShell script

Problem:
Currently I am working on BizTalk 2013 project after development and deployment I am responsible to administrator the BizTalk Production environment. Often some receive locations go down and processing of urgent messages stopped processing, Here is need of something that brings the ports(s) up.

Solution:
PowerShell can solve the above issue. If we use PowerShell script it will check the status of specific receive location either it is enable or disable. If the receive location is already enable do nothing but if the receive location is disabled, I want the status of disabled receive location through email and restart the host to which that receive location is associated and enable the receive location. After successful enabling of receive location receive successful email.

Configure PowerShell

1.      Open “windows PowerShell” ‘with run as administrator’

2.      Type the command “Set-ExecutionPolicy Unrestricted” and hit enter.

PowerShell Script:
We will need to open PowerShell IDE for the development of PowerShell scripts.

1.      To open the PowerShell IDE go to Start => All Programs => Accessoires => Windows PowerShell -> windows PowerShell ISE.

2.      BizTalk.ExplorerOM and WMI will used to communicate to the BizTalk environment.

3.      There is need of functions to manage what we want, i.e. checking the status of receive location, sending status of receive location, restarting host instance and enabling the receive location:
 
# 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 = "BizTalkServerApplication" #hostname for the JMS host
$rcvLocation = "GetNewConfirmReceiveLocation" #receive location
#Email Config
$emailFrom = "Bz_admin@XXXX.com.sa"
$emailTo = "mmasood@XXXX.com.sa"
$emailSubject  = "Receive Location 'GetNewConfirmReceiveLocation' -Service: 8 on BIZTALK PROD is DOWN"
$emailSubject2 = "Receive Location 'GetNewConfirmReceiveLocation' -Service: 8 BIZTALK PROD is UP"
$emailServer ="Mail.XXXX.com.sa"
#Function to retrieve 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.Enable()
        $recvloc = get-wmiobject MSBTS_ReceiveLocation `
       -namespace 'root\MicrosoftBizTalkServer' `
       -filter "Name='GetNewConfirmReceiveLocation'"
       [void]$recvloc.Enable()
       [void]$Catalog.Refresh()
   }
#Function to sends an error email
function sendEmail(){
$message = "GetNewConfirmReceiveLocation is Down. Trying to restart the host and enabling the receive location"
    $smtp=new-object Net.Mail.SmtpClient($emailServer)
    $smtp.Send($emailFrom, $emailTo, $emailSubject, $message)
 }
#Function to sends an ok email
function sendEmailok(){
$messagee = "GetNewConfirmReceiveLocation is brought up successfully"
$smtp=new-object Net.Mail.SmtpClient($emailServer)
$smtp.Send($emailFrom, $emailTo, $emailSubject2, $messagee)
}
$keeplooping = $true
$isok = $false
$i = 0;
#check status 5 times
$isEnabled = getStatus
if($isEnabled -eq $false){
while($keeplooping -eq $true){
$isEnabled = getStatus
if($isEnabled -eq $false){
sendEmail
#Restart host
Restart-Service -Displayname "BizTalk Service BizTalk Group : ${hostname}"
#wait a few seconds
Start-Sleep -s 30 
#Enable receive location
enableReceiveLocation
#Wait 5 minutes
Start-Sleep -s 30
$i++;
        if($i -gt 4){
            $keeplooping = $false
        }
    }else{
        sendEmailok
        $keeplooping = $false
        $isok = $true
    }
}
}
This PowerShell script will check the status of a receive location if receive location is already enabled script will do nothing. If the receive location is disabled then script will first send an error email stating receive location is down\disabled then tries 3 attempts to restart the host instance and enabling the receive location. Once host instance restarted successfully and port enabled successfully it will again send a successfully email stating the receive location XXXXXXXXX is enabled successfully.
After testing and verification of above script then we may put this under windows scheduler. OR put the code in SQL Agent.

 
 


 
 

6 comments:

  1. Its a very nice information on your blog. so keep posting. you may also like it
    Biztalk Online Training

    ReplyDelete
  2. thanks for providing the information about biztalk, it is very effctive and very useful to me keep update your articles..please check it once Biztalk Online Training Hyderabad

    ReplyDelete
  3. Thanks for the post, very useful. Would like to suggest best training on hadoop visit
    Biztalk Online Training hyderabad

    ReplyDelete
  4. Hi,
    I can start or stop send Port but not ReceiveLocation with wmiObject. What could be the reason?

    ReplyDelete
  5. Thanks for writing this in-depth post. You covered every angle.

    Biztalk Online Training Hyderabad

    ReplyDelete
  6. This is very nice blog,and it is helps for developers.Thanks for sharing
    Biztalk

    Online course Bangalore

    ReplyDelete