I wanted a script that post a new BizTalk build, I could run and it could create new hosts, host instances, create new receive and send handlers and finally start the host instances
'Create the new hostCreateHost "App2Host", InProcess, "CORP\BTSAppUsers-Dev", False, False, False, False'Make the new host the default hostUpdateHost "App2Host",True,True
'Turn the tracking off of the initially installed hostUpdateHost "App1Host",False,False
'Create the host instanceFinalizeInstallHostInstance "App2Host","CORP\svcBTSHost-Dev","P4ssw0rd!"ReConfigureReceiveHandler "App1Host","WCF-SQL"CreateReceiveHandler "App2Host","WCF-SQL"CreateSendHandler "App2Host","WCF-SQL"'Start all of the host instances that are not currently running
StartAllInProcessHostInstanceSub CreateHost (HostName, HostType, NTGroupName, AuthTrusted, Isdefault, IsHost32BitOnly, HostTracking )
On Error Resume NextDim objLocator, objService, objHostSetting, objHS
' Connects to local server WMI Provider BizTalk namespace
Set objLocator = Createobject ("wbemScripting.SWbemLocator")Set objService = objLocator.ConnectServer(".", "root/MicrosoftBizTalkServer")' Get WMI class MSBTS_HostSettingSet objHostSetting = objService.Get ("MSBTS_HostSetting")Set objHS = objHostSetting.SpawnInstance_
objHS.Name = HostNameobjHS.HostType = HostTypeobjHS.NTGroupName = NTGroupNameobjHS.AuthTrusted = AuthTrustedobjHS.Isdefault = IsDefaultobjHS.IsHost32BitOnly = IsHost32BitOnlyobjHS.HostTracking = HostTracking' Create instanceobjHS.Put_(CreateOnly)CheckWMIErrorwscript.echo "Host - " & HostName & " - has been created successfully"end SubSub UpdateHost (HostName, HostTracking, IsDefault)
On Error Resume NextDim objLocator, objService, objHS
' Connects to local server WMI Provider BizTalk namespace
Set objLocator = Createobject ("wbemScripting.SWbemLocator")Set objService = objLocator.ConnectServer(".", "root/MicrosoftBizTalkServer")' Look for WMI Class MSBTS_HostSetting with name equals HostName valueSet objHS = objService.Get("MSBTS_HostSetting.Name='" & HostName & "'")objHS.HostTracking = HostTrackingobjHS.IsDefault = IsDefault' Update instance propertiesobjHS.Put_(UpdateOnly)' Check for error condition before continuing.CheckWMIErrorwscript.echo "Host - " & HostName & " - has been updated successfully"end SubSub FinalizeInstallHostInstance (HostName, uid, pwd)
On Error Resume NextDim ServerName, objSysInfo
Set objSysInfo = CreateObject( "WinNTSystemInfo" )ServerName = objSysInfo.ComputerNameCheckWMIErrorMapInstallHostInstance HostName,ServerName,uid,pwdend SubSub MapInstallHostInstance (HostName, ServerName, uid, pwd)
'Sub MapInstallHostInstance (HostName, uid, pwd)
On Error Resume NextDim objLocator, objService, objServerHost, objSH
Dim objHostInstance, objHI
'Dim ServerName, wshShell
' Connects to local server WMI Provider BizTalk namespace
Set objLocator = Createobject ("wbemScripting.SWbemLocator")Set objService = objLocator.ConnectServer(".", "root/MicrosoftBizTalkServer")Set objServerHost = objService.Get ("MSBTS_ServerHost")Set objSH = objServerHost.SpawnInstance_
objSH.HostName = HostNameobjSH.ServerName = ServerName' Invoke MSBTS_ServerHost Map methodobjSH.MapCheckWMIErrorwscript.echo "Host - " & HostName & " - has been mapped successfully to server - " & ServerNameSet objHostInstance = objService.Get ("MSBTS_HostInstance")Set objHI = objHostInstance.SpawnInstance_
objHI.Name = "Microsoft BizTalk Server " & HostName & " " & ServerName' Invoke MSBTS_HostInstance Install methodobjHI.Install uid, pwd, true ' Calling MSBTS_HostInstance::Install(string Logon, string Password, boolean GrantLogOnAsService) methodCheckWMIErrorwscript.echo "HostInstance - " & HostName & " - has been installed successfully on server - " & ServerNameend SubSub ReConfigureReceiveHandler(HostName,AdapterName)
'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'Get the command line arguments entered for the scriptDim objArgs: Set objArgs = WScript.Arguments'Make sure the expected number of arguments were provided on the command line.
'if not, print usage text and exit.Dim objInstSet, objInst, strQuery
'set up a WMI query to acquire a list of send handlers with the given Name key value.'This should be a list of zero or one send handlers.
strQuery = "SELECT * FROM MSBTS_ReceiveHandler WHERE AdapterName =""" & AdapterName & """"Set objInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(strQuery)'If send handler found, set configuration information, otherwise print error and end.If objInstSet.Count > 0 thenFor Each objInst in objInstSetobjInst.HostNameToSwitchTo=HostName'Commit the change to the databaseobjInst.Put_(UpdateOnly)If Err <> 0 ThenPrintWMIErrorThenExit Err.Description, Err.NumberEnd IfWScript.Echo "The "& AdapterName &" Receive Handler was successfully configured."Next
Else
WScript.Echo "No Receive Handler was found matching that AdapterName."
End IfEnd Sub' Sample to show MSBTS_ReceiveHandler instance creation with CustomCfg propertySub CreateReceiveHandler (HostName, AdapterName)
On Error Resume NextDim objLocator, objService, objReceiveHandler, objRH, objSendHandler, objSH
' Connects to local server WMI Provider BizTalk namespace
Set objLocator = Createobject ("wbemScripting.SWbemLocator")Set objService = objLocator.ConnectServer(".", "root/MicrosoftBizTalkServer")' Get WMI class MSBTS_ReceiveHandlerSet objReceiveHandler = objService.Get ("MSBTS_ReceiveHandler")Set objRH = objReceiveHandler.SpawnInstance_
objRH.AdapterName = AdapterNameobjRH.HostName = HostName' Create instanceobjRH.Put_(CreateOnly)CheckWMIErrorwscript.echo "Receive Handler - " & AdapterName & " " & HostName & " - has been created successfully"end SubSub CreateSendHandler (HostName, AdapterName)
On Error Resume NextDim objLocator, objService, objSendHandler, objSH
' Connects to local server WMI Provider BizTalk namespace
Set objLocator = Createobject ("wbemScripting.SWbemLocator")Set objService = objLocator.ConnectServer(".", "root/MicrosoftBizTalkServer")' Get WMI class MSBTS_ReceiveHandler'Insert the Send Handler make sure you use SendHandler2 as SendHandler is a throwback to BTS 2004 which does not allow updatesSet objSendHandler = objService.Get ("MSBTS_SendHandler2")Set objSH = objSendHandler.SpawnInstance_
objSH.AdapterName = AdapterNameobjSH.HostName = HostName' Create instanceobjSH.Put_(CreateOnly)CheckWMIErrorwscript.echo "Send Handler - " & AdapterName & " " & HostName & " - has been created successfully"end SubSub StartAllInProcessHostInstance ()
On Error Resume NextDim Query, HostInstSet, Inst
' Enumerate all InProcess type Host InstanceQuery = "SELECT * FROM MSBTS_HostInstance WHERE HostType =1"
Set HostInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)For Each Inst in HostInstSet' If host instance is stopped, then it'll start itIf( HostInstServiceState_Stopped = Inst.ServiceState ) Thenwscript.echo "Starting host instance..."
Inst.Start ' Calling MSBTS_HostInstance::Start() methodCheckWMIErrorwscript.echo "HostInstance - " & Inst.HostName & " - has been started successfully on server - " & Inst.RunningServerEnd 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