Jan 052011
 

Wanted to put a couple of scripts I put together on how to start and stop host instances using WMI in a VBS file

StartAllInProcessHostInstance
const HostInstServiceState_Stopped=1
Sub StartAllInProcessHostInstance ()
   On Error Resume Next
   Dim Query, HostInstSet, Inst
   ' Enumerate all InProcess type Host Instance
   Query = "SELECT * FROM MSBTS_HostInstance WHERE HostType =1 and IsDisabled='FALSE'"
   Set HostInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
   For Each Inst in HostInstSet
      ' If host instance is stopped, then it'll start it
      If( Inst.ServiceState=HostInstServiceState_Stopped ) Then
         wscript.echo "Starting host instance -> " & Inst.HostName
             Inst.Start   ' Calling MSBTS_HostInstance::Start() method
         CheckWMIError
         wscript.echo Inst.HostName & " has been started successfully on server " & Inst.RunningServer & VBNewLine
      End If
   Next
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   CheckWMIError()
   If Err <> 0   Then
      On Error Resume   Next
      Dim strErrDesc: strErrDesc = Err.Description
      Dim ErrNum: ErrNum = Err.Number
      Dim WMIError : Set WMIError = CreateObject("WbemScripting.SwbemLastError")
      If ( TypeName(WMIError) = "Empty" ) Then
         wscript.echo strErrDesc & " (HRESULT: "   & Hex(ErrNum) & ")."
      Else
         wscript.echo WMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."
         Set WMIError = nothing
      End   If
      wscript.quit 0
   End If
End Sub

StopAllInProcessHostInstance
const HostInstServiceState_Started=4
Sub StopAllInProcessHostInstance ()
   On Error Resume Next
   Dim Query, HostInstSet, Inst
   ' Enumerate all InProcess type Host Instance
   Query = "SELECT * FROM MSBTS_HostInstance WHERE HostType =1 and IsDisabled='FALSE'"
   Set HostInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
   For Each Inst in HostInstSet
      ' If host instance is stopped, then it'll start it
      If( Inst.ServiceState=HostInstServiceState_Started ) Then
         wscript.echo "Stopping host instance -> " & Inst.HostName
             Inst.Stop   ' Calling MSBTS_HostInstance::Stop() method
         CheckWMIError
         wscript.echo Inst.HostName & " has been stopped successfully on server " & Inst.RunningServer & VBNewLine
      End If
   Next
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   CheckWMIError()
   If Err <> 0   Then
      On Error Resume   Next
      Dim strErrDesc: strErrDesc = Err.Description
      Dim ErrNum: ErrNum = Err.Number
      Dim WMIError : Set WMIError = CreateObject("WbemScripting.SwbemLastError")
      If ( TypeName(WMIError) = "Empty" ) Then
         wscript.echo strErrDesc & " (HRESULT: "   & Hex(ErrNum) & ")."
      Else
         wscript.echo WMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."
         Set WMIError = nothing
      End   If
      wscript.quit 0
   End If
End Sub