VBScript to Start BizTalk Orchestrations

This blog contains a sample VBScript to start a BizTalk Orchestration

The SDK (Software Development Kit) of BizTalk includes a sample to stop orchestrations.
“c:\Program Files (x86)\Microsoft BizTalk Server 2010\SDK\Samples\Admin\WMI\Stop Orchestration\VBScript\StopOrch.vbs”
I guess, since it is just a sample, they chose not to provide the opposite to “Start” orchestrations.

I was having to deploy just a .DLL, not entire MSI over and over, so I started to make a script to do it. I need to stop the orchestration, run BTSTask with the AddResource option, then start the orchestration.

The trick is that you have to Enlist it before Starting it. So I run both the .Enlist and the .Start methods, one right after the other. I removed the optional UNENLIST parm.

Below is the VBScript:

' ScriptName: StartOrch.vbs 
' WMI script to start a specific orchestration.
' Neal Walters - 02/10/2016 took StopOrch.vbs in BizTalk SDK to make StartOrch.vbs 
' This file is based on samples in the Microsoft BizTalk Server 2009 SDK

Option Explicit

Sub StartOrch()
  'Get the command line arguments entered for the script
  Dim objArgs: Set objArgs = WScript.Arguments

  'error handling is done by explicity checking the err object rather than using
  'the VB ON ERROR construct, so set to resume next on error.
  on error resume next
  'Make sure the expected number of arguments were provided on the command line.
  'if not, print usage text and exit.
  If (objArgs.Count <> 2) Then
    wscript.quit 0
  End If

  Dim InstSet, Inst, Query, OrchestrationName, AssemblyName, Unenlist
  Dim AutoDisableReceiveLocation: AutoDisableReceiveLocation = 2
  Dim AutoSuspendOrchestrationInstance: AutoSuspendOrchestrationInstance = 2
  OrchestrationName = objArgs(0)
  AssemblyName = objArgs(1)
  'set up a WMI query to acquire a list of orchestrations with the given Name and 
  'AssemblyName key values.  This should be a list of zero or one Orchestrations.
  WScript.Echo "Orch=" & OrchestrationName
  WScript.Echo "AssemblyName=" & AssemblyName 
   Query = "SELECT * FROM MSBTS_Orchestration WHERE Name =""" & OrchestrationName & """ AND AssemblyName = """ & AssemblyName & """"
  WScript.Echo "Query=" & Query 
  Set InstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
  'Check for error condition before continuing.
  If Err <> 0  Then
    PrintWMIErrorThenExit Err.Description, Err.Number
  End If

  'If orchestration found, enlist the orchestration, otherwise print error and end.
  If InstSet.Count > 0 then
    For Each Inst in InstSet
      If Err <> 0  Then
        PrintWMIErrorThenExit Err.Description, Err.Number
      End If
      If Err <> 0  Then
        PrintWMIErrorThenExit Err.Description, Err.Number
      End If
      wscript.echo "The Orchestration was successfully started."
    wscript.echo "No orchestration was found matching that Name and AssemblyName. InstSet.Count=0"
  End If
End Sub 

'This subroutine deals with all errors using the WbemScripting object.  Error descriptions
'are returned to the user by printing to the console.
Sub  PrintWMIErrorThenExit(strErrDesc, ErrNum)
  On Error Resume  Next
  Dim  objWMIError  : Set objWMIError =  CreateObject("WbemScripting.SwbemLastError")

  If ( TypeName(objWMIError) = "Empty" ) Then
    wscript.echo strErrDesc & " (HRESULT: "  & Hex(ErrNum) & ")."
    wscript.echo objWMIError.Description & "(HRESULT: "  & Hex(ErrNum) & ")."
    Set objWMIError  = nothing
  End  If
  'bail out
  wscript.quit 0
End Sub 

Sub PrintUsage()
  WScript.Echo "Usage:" + Chr(10) + Chr(10) + _
         "cscript StartOrch.vbs <Orchestration Name> <Assembly Name>" + _  
         Chr(10) + Chr(10) + "Where: " + Chr(10) + _
         "<Orchestration Name> = The name of the orchestration you wish to enlist." + _
         Chr(10) + "       Example: 'MyBusinessOrchestration'" + Chr(10) + Chr(10) + _
         "<Assembly Name>      = The name of the assembly in which the orchestration was deployed." + _
         Chr(10) + "       Example: 'MyBusinessAssembly'" + Chr(10) + Chr(10) + _
         Chr(10) + "       Example: 'Unenlist'" + Chr(10) + Chr(10)
End Sub


To run it:
cscript “VBScript\StartOrch.vbs” “ProjNamespace.OrchName” “AppName”

Leave a Reply

Recent Posts




All Rights Reserved Theme by 404 THEME.