BizTalk Tips & Things

Agustin Mantaras BizTalk Blog

Extended vbs to start/stop all BizTalk host instances in several servers


Warning: do not try this directly in production without test it!!!  Sonrisa

 Some services may require Administrator rights to be Started / Stopped

‘ VBScript ActBizTalkServerInstance.vbs
‘ Sample script to Stop Or Start ALL BizTalk Services, it will return 0 if no errors found, 1 if any, and will exit when the error occurs (it will not finish the work).
‘ The Following example will stop all BizTalkHost instances in Server 1, 2 and 3
‘ SAMPLE USE: cScript ActBizTalkServerInstance STOP;Server1;Server2;Server3   (Note: without “;” at the end)
‘ Valid actions are : START or STOP
‘ Created by Agustín Mántaras Sept 2014
‘ ——————————————————-‘

Dim objWMIService, objItem, objService, str, ReturnCode,i, Action
Dim colListOfServices, strService, intSleep ,sComputers,g_aComputers
strComputer = “.”


On Error Resume Next


Sub Main
    ReturnCode = 1 ‘By default the exit Code will be 1–> error

    For i = 0 to UBound(g_aComputers)
        ‘Looping through the parameters to get stop BizTalk Servers host intances in those machines

        if i = 0 Then
           ‘Parameter 0 should be the action to run –> START OR STOP
            StrAction = g_aComputers(i)
            if strAction <> “START” Then
                if strAction <> “STOP” Then
                    ReturnCode = 1
                end if
            end if
            ActBizTalkServices g_aComputers(i),strAction
        End if
        if ReturnCode <> 0 then
            Exit For
        End if

    WScript.Echo  ReturnCode  ‘Returning 0 or 1
    WScript.Quit  (ReturnCode)
End Sub ‘Main

Sub ActBizTalkServices (strComputer,strAction)
    ‘ NB strService is case sensitive.
    On Error Goto 0
    On Error Resume Next
    strService = ” ‘BTSSvc%’ ”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” _
    & strComputer & “\root\cimv2”)
    Set colListOfServices = objWMIService.ExecQuery _
    (“Select * from Win32_Service Where Name like “_
    & strService & ” “)
    if err.Number = 0 Then
    ‘Something Went wrong when getting server names
        ReturnCode = 0

        For Each objService in colListOfServices
            On error GoTo 0
            On Error Resume Next
            IF strAction = “STOP” Then
            end if
            if err.Number = 0 then
               ReturnCode = 0  ‘The action was sussecfully

                ‘Getting out the For loop since we get an error stopping a BizTalk Service
                ReturnCode = err.number
                Exit for          
            end if
        ReturnCode = 1
    end if
End Sub ‘ActBizTalkServices


Sub ProcessArguments()
    sSyntax = “CScript StopBizTalkServers.vbs ACTION [START/STOP];<computer[;computer]>” & vbNewLine & _
    ” <computer[;computer]>  List of computers to create and start the perfmon log.” & vbNewLine
    Set oArgs = WScript.Arguments
    SELECT CASE oArgs.Count
        CASE 0
            WScript.Echo sSyntax
        CASE 1
            sComputers = oArgs(0)
        CASE Else
            WScript.Echo sSyntax
    ‘ Change any localhost entryies to be a period for WMI.
    g_aComputers = Split(sComputers, “;”)
    For i = 0 to UBound(g_aComputers)
        If LCase(g_aComputers(i)) = “localhost” Then
            g_aComputers(i) = “.”
        End If
End Sub ‘ProcessArguments