Nov 102010
 

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 host
CreateHost "App2Host", InProcess, "CORP\BTSAppUsers-Dev", False, False, False, False

'Make the new host the default host
UpdateHost "App2Host",True,True

'Turn the tracking off of the initially installed host
UpdateHost "App1Host",False,False

'Create the host instance
FinalizeInstallHostInstance "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
StartAllInProcessHostInstance

Sub CreateHost (HostName, HostType, NTGroupName, AuthTrusted, Isdefault, IsHost32BitOnly, HostTracking )
   On Error Resume Next

   Dim 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_HostSetting
   Set objHostSetting = objService.Get ("MSBTS_HostSetting")

   Set objHS = objHostSetting.SpawnInstance_

   objHS.Name = HostName
   objHS.HostType = HostType
   objHS.NTGroupName = NTGroupName
   objHS.AuthTrusted = AuthTrusted
   objHS.Isdefault = IsDefault
   objHS.IsHost32BitOnly = IsHost32BitOnly
   objHS.HostTracking = HostTracking

   ' Create instance
   objHS.Put_(CreateOnly)

   CheckWMIError
   wscript.echo "Host - " & HostName & " - has been created successfully"

end Sub

Sub UpdateHost (HostName, HostTracking, IsDefault)
   On Error Resume Next

   Dim 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 value
   Set objHS = objService.Get("MSBTS_HostSetting.Name='" & HostName & "'")

   objHS.HostTracking = HostTracking
   objHS.IsDefault = IsDefault

   ' Update instance properties
   objHS.Put_(UpdateOnly)

   ' Check for error condition before continuing.
   CheckWMIError
   wscript.echo "Host - " & HostName & " - has been updated successfully"

end Sub
Sub FinalizeInstallHostInstance (HostName, uid, pwd)
 On Error Resume Next
 Dim ServerName, objSysInfo
 Set objSysInfo = CreateObject( "WinNTSystemInfo" )
 ServerName = objSysInfo.ComputerName
 CheckWMIError
 MapInstallHostInstance HostName,ServerName,uid,pwd
end Sub

Sub MapInstallHostInstance (HostName, ServerName, uid, pwd)
'Sub MapInstallHostInstance (HostName, uid, pwd)
   On Error Resume Next

   Dim 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 = HostName
   objSH.ServerName = ServerName

   ' Invoke MSBTS_ServerHost Map method
   objSH.Map

   CheckWMIError
   wscript.echo "Host - " & HostName & " - has been mapped successfully to server - " & ServerName

   Set objHostInstance = objService.Get ("MSBTS_HostInstance")

   Set objHI = objHostInstance.SpawnInstance_

   objHI.Name = "Microsoft BizTalk Server " & HostName & " " & ServerName

   ' Invoke MSBTS_HostInstance Install method
   objHI.Install uid, pwd, true   ' Calling MSBTS_HostInstance::Install(string Logon, string Password, boolean GrantLogOnAsService) method

   CheckWMIError
   wscript.echo "HostInstance - " & HostName & " - has been installed successfully on server - " & ServerName

end Sub
Sub 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 script
	Dim 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 then
		For Each objInst in objInstSet
			objInst.HostNameToSwitchTo=HostName


			'Commit the change to the database
			objInst.Put_(UpdateOnly)
			If Err <> 0	Then
				PrintWMIErrorThenExit Err.Description, Err.Number
			End If
			WScript.Echo "The "& AdapterName &" Receive Handler was successfully configured."
		Next
	Else
		WScript.Echo "No Receive Handler was found matching that AdapterName."
	End If
End Sub
' Sample to show MSBTS_ReceiveHandler instance creation with CustomCfg property
Sub CreateReceiveHandler (HostName, AdapterName)
   On Error Resume Next

   Dim 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_ReceiveHandler
   Set objReceiveHandler = objService.Get ("MSBTS_ReceiveHandler")

   Set objRH = objReceiveHandler.SpawnInstance_

   objRH.AdapterName = AdapterName
   objRH.HostName = HostName

   ' Create instance
   objRH.Put_(CreateOnly)

   CheckWMIError
   wscript.echo "Receive Handler - " & AdapterName & " " & HostName & " - has been created successfully"

end Sub
Sub CreateSendHandler (HostName, AdapterName)
   On Error Resume Next

   Dim 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 updates
   Set objSendHandler = objService.Get ("MSBTS_SendHandler2")

   Set objSH = objSendHandler.SpawnInstance_

   objSH.AdapterName = AdapterName
   objSH.HostName = HostName

   ' Create instance
   objSH.Put_(CreateOnly)

   CheckWMIError
   wscript.echo "Send Handler - " & AdapterName & " " & HostName & " - has been created successfully"

end Sub
Sub StartAllInProcessHostInstance ()
   On Error Resume Next

   Dim Query, HostInstSet, Inst

   ' Enumerate all InProcess type Host Instance
   Query = "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 it
      If( HostInstServiceState_Stopped = Inst.ServiceState ) Then
         wscript.echo "Starting host instance..."
             Inst.Start   ' Calling MSBTS_HostInstance::Start() method
         CheckWMIError
         wscript.echo "HostInstance - " & Inst.HostName & " - has been started successfully on server - " & Inst.RunningServer
      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


			
  • Mohammad

    Thanks for taking time and making the script available.
    I’m trying to make my BizTalk implementation as one-click installation/unistallation.

    Could you tell me how can I assign Host Instance when I want to Enlist an Orchestration/ReceiveLocation/Send Port in VB/C# script?

    Thanks in advance,
    Mohammad

  • Mark

    Hi,
    many thanks for the script, i have test it on W2k8r2 BizTalk2010 installation.

    If i try to run the script with the commands in it i got the error-message Hostype is empty.

    If i try to run it with cscript and command CreateHost with parameters i got no error message but the Host will not be created.

    Some hints for me ?

    Mark