Wanted to put a couple of scripts I put together on how to start and stop host instances using WMI in a VBS file
StartAllInProcessHostInstanceconst HostInstServiceState_Stopped=1
Sub StartAllInProcessHostInstance ()
On Error Resume NextDim Query, HostInstSet, Inst
' Enumerate all InProcess type Host InstanceQuery = "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 itIf( Inst.ServiceState=HostInstServiceState_Stopped ) Thenwscript.echo "Starting host instance -> " & Inst.HostName
Inst.Start ' Calling MSBTS_HostInstance::Start() methodCheckWMIErrorwscript.echo Inst.HostName & " has been started successfully on server " & Inst.RunningServer & VBNewLine
End IfNext
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 ThenOn Error Resume NextDim strErrDesc: strErrDesc = Err.DescriptionDim ErrNum: ErrNum = Err.NumberDim WMIError : Set WMIError = CreateObject("WbemScripting.SwbemLastError")If ( TypeName(WMIError) = "Empty" ) Thenwscript.echo strErrDesc & " (HRESULT: " & Hex(ErrNum) & ")."Else
wscript.echo WMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."Set WMIError = nothingEnd Ifwscript.quit 0End IfEnd SubStopAllInProcessHostInstanceconst HostInstServiceState_Started=4
Sub StopAllInProcessHostInstance ()
On Error Resume NextDim Query, HostInstSet, Inst
' Enumerate all InProcess type Host InstanceQuery = "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 itIf( Inst.ServiceState=HostInstServiceState_Started ) Thenwscript.echo "Stopping host instance -> " & Inst.HostName
Inst.Stop ' Calling MSBTS_HostInstance::Stop() methodCheckWMIErrorwscript.echo Inst.HostName & " has been stopped successfully on server " & Inst.RunningServer & VBNewLine
End IfNext
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 ThenOn Error Resume NextDim strErrDesc: strErrDesc = Err.DescriptionDim ErrNum: ErrNum = Err.NumberDim WMIError : Set WMIError = CreateObject("WbemScripting.SwbemLastError")If ( TypeName(WMIError) = "Empty" ) Thenwscript.echo strErrDesc & " (HRESULT: " & Hex(ErrNum) & ")."Else
wscript.echo WMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."Set WMIError = nothingEnd Ifwscript.quit 0End IfEnd Sub