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”

I was working with an old third party tool that only supported VBScript for programming.  I needed to email a file if the file had more than one row.  All the CSV (Comma Separated Value) files created had the CSV headers on the first line. Basically, this was an error/exception report, so I only wanted to send an email if there was more than one line in the file. The following basically counts the number of Carriage-Return/Line-Feed characters.

set fso=CreateObject("Scripting.FileSystemObject")

fa = "E:\MESSAGES\ACER\PROD\Batch\Out\database_failed_POs_20150916_140035.csv"
countRows = CountRowsInFile(fa)
msgbox "File with Error(s) Only countRows=" &amp; countRows 

fa = "E:\MESSAGES\ACER\PROD\Batch\Out\database_failed_POs_20150916_150009.csv"
countRows = CountRowsInFile(fa)
msgbox "Header Only countRows=" &amp; countRows 

Function CountRowsInFile(filename)
	Dim oFso, oReg, sData, lCount
	Const ForReading = 1
	Set oReg = New RegExp
	sData = Fso.OpenTextFile(filename, ForReading).ReadAll
	With oReg
	    .Global = True
	    .Pattern = "\r\n" 'vbCrLf
	    '.Pattern = "\n" ' vbLf, UTF-8 encoded text file?
	    lCount = .Execute(sData).Count 
	End With
	Set oReg = Nothing
	CountRowsInFile = lCount 
End Function 


Above was modeled after post on StackOverflow (basically turned it into a Function).

Below is just a code fragment that creates a date useful for putting on a filename. It’s not particular elegant, and there are other ways to do the same thing (for example using the PAD function). But it works and it’s easy to read and change.

dtYear  = year(date) 
dtMonth = Right(String(2,"0") &amp; Month(date), 2)
dtDay   = Right(String(2,"0") &amp; Day(date), 2)
dtHour  = Right(String(2,"0") &amp; Hour(now()), 2)
dtMin   = Right(String(2,"0") &amp; Minute(now()), 2)
dtSec   = Right(String(2,"0") &amp; Second(now()), 2)
dtYYYYMMDDHHMM = dtYear &amp; dtMonth &amp; dtDay &amp; dtHour &amp; dtMin 
dtYYYYMMDDHHMM2 = dtYear &amp; "_" &amp; dtMonth &amp; "_" &amp; dtDay &amp; "_" &amp; dtHour &amp; "_" &amp;  dtMin 
dtYYYYMMDDHHMMSS = dtYear &amp; dtMonth &amp; dtDay &amp; dtHour &amp; dtMin &amp; dtSec
dtYYYYMMDDHHMMSS2 = dtYear &amp; dtMonth &amp; dtDay &amp; "_" &amp; dtHour &amp; dtMin &amp; dtSec

'example of how the above can be used to insert a date in a filename.
DTSOutFilenameWithDate = replace(DTSOutFilename, "_.csv", "_" &amp; dtYYYYMMDDHHMM  &amp; ".csv")

See also related VBScript blog: Deleting files in a folder over x days old (in VBScript)

Yes, it’s the year 2015, and I still find myself creating VBScripts from time to time. My client has an old server that uses a third party tool to do database extracts, and it supports only VBScript. So we have scripts that email CSV (Comma Separated Value) extracts, and/or copy them to various folders on different servers (or to FTP sites).

I wanted to make sure a script cleaned-up old files that were created, including Trace files. Normally you want a retention period of x days. So I created a function that takes three parms:

  1. Days to delete (if older than)
  2. Folder Path
  3. File Mask (only delete files containing this string)
' Neal Walters  09/17/2015 

countFilesDeleted = DeleteFiles(7,"E:\MESSAGES\Folder1","PlanITROI_Hourly_Order_Status_") 
WScript.Echo("Count of Files Deleted = " &amp; countFilesDeleted) 
countFilesDelested = DeleteFiles(21,"E:\MESSAGES\Folder2\VBScriptsTrace","Trace") 
WScript.Echo("Count of Files Deleted = " &amp; countFilesDeleted) 

Function DeleteFiles(Days,FolderName,FileMask) 
   set fso2 = CreateObject("Scripting.FileSystemObject")
   set f = fso2.GetFolder(FolderName) 
   set fc = f.Files 
   countDeletes = 0 
   for each f1 in fc 
      if DateDiff("d", f1.DateCreated, Date) &gt; Days Then
	     if instr(f1.Name,FileMask) &gt; 0 then 
			WScript.Echo "Deleting File: " &amp; f1.Name 
			countDeletes = countDeletes + 1 
		 end if 
      end if
    Set fso2 = Nothing 	
	DeleteFiles = countDeletes 
End Function 


Compare to Powershell cleanup/delete files script.