06

Feb

2009

WMI script to update send handler host

In BizTalk Server 2004, sometime we need to update host for a send handler, the script below will do the job.


Option Explicit

' wbemChangeFlagEnum Setting
const UpdateOnly = 1

UpdateSendHandler
' MSBTS_ReceiveHandler host association update
Sub UpdateSendHandler ()
	'Get the command line arguments entered for the script
	Dim Args: Set Args = WScript.Arguments

	'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
	
	'Make sure the expected number of arguments were provided on the command line.
	'if not, print usage text and exit.
	If (Args.Count <> 2) Then
		PrintUsage()
		wscript.quit 0
	End If

	Dim AdapterName, HostNameToSwitchTo

	AdapterName = Args(0)
	HostNameToSwitchTo = Args(1)

   Dim Query, ReceiveHandlerInstSet, Inst

   ' Look for the target WMI Class MSBTS_ReceiveHandler instance
   Query = "SELECT * FROM MSBTS_SendHandler WHERE AdapterName =""" & AdapterName & """"
   Set ReceiveHandlerInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
   
   If ReceiveHandlerInstSet.Count > 0 Then

      For Each Inst In ReceiveHandlerInstSet
         ' Update host association
         Inst.HostName = HostNameToSwitchTo
         Inst.Put_(UpdateOnly)
         
         ' Check for error condition before continuing.
         CheckWMIError
         wscript.echo "Receive Handler - " & AdapterName & " " & HostNameToSwitchTo & " - has been updated sucessfully"
      Next
   Else
      wscript.echo "Receive Handler - " & AdapterName & " - cannot be found"
      wscript.quit 0
   End If

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

Sub PrintUsage()
	WScript.Echo "Usage:" + Chr(10) + Chr(10) + _
				 "cscript UpdateSendHandlerHost.vbs [Adapter Name] [Host Name To Switch To]" + _
				 Chr(10) + Chr(10) + " Where: " + Chr(10) + _
				 "  [Adapter Name]           = The name of the adapter you wish to update." + _
				 Chr(10) + "       Example: 'MyFTPAdapter'" + Chr(10) + Chr(10) + _
				 "  [Host Name To Switch To] = The name of the BizTalk host to be applied to this adapter." + _
				 Chr(10) + "       Example: 'MyFTPHost'" + Chr(10) + Chr(10)
End Sub

Comments are closed.