Tuesday, October 23, 2007

Script to Get Running BizTalk Host Instance Name

Code snippet dump!  Our enterprise monitoring software needed to be able to iterate the running instances of BizTalk so that it could sample the appropriate performance counters.  This is the 'alpha' version we are running with.    Because of the way the performance counters are registered on a per instance basis, we needed the 'name' of the host and not necessarily the GUID associated with it.  Since administrators could potentially move host instances from node to node, or create, etc (however unlikely) we wanted something that we wouldn't have to remember to maintain if an administrator changed the location of a  host instance at 3am.

Other proposed options included:

  1. Iterate the BTSNTSvc.exe process and parsing the command-line looking for the host name - this tells us the running instance.
  2. Use the BizTalk WMI model to query for the information we needed - this is fairly rich object model.
  3. Iterate the HKLM\CurrentControlSet\Services registry node, looking for nodes that started with 'BTSNTSvc$' and parsing that value, though this didn't give us whether the host instance was actually running.

The code below expects to execute on the machine its querying for, a requirement of the execution model of our monitoring software.

Code: List Running BizTalk Host Instance Names

Option Explicit
 
' 
' MSBTS_HostInstance.ServiceState valid values
' More information regarding MSBS_HostInstance can be found on MSDN http://msdn2.microsoft.com/en-us/library/aa560660.aspx
' 
'Stopped:1
'Start pending:2
'Stop pending:3
'Running:4
'Continue pending:5
'Pause pending:6
'Paused:7
'Unknown:8
 
Main
 
 
Sub Main
 
    dim hosts
    dim host
    dim computerName
    
    computerName = GetLocalComputerName()
   
   ' returns a wmi object set of MSBTS_HostInstance objects
   '
    Set hosts = GetRunningBizTalkHosts( computerName )
    
    
    for each host in hosts
        WScript.Echo host.HostName
        ' do something for each host
    next
 
    
   
    
End Sub
 
Function GetLocalComputerName()
    dim wmi
    dim systemInfo
    dim objItem
    dim propertyName
    dim computerName
    
    set wmi = GetObject("winmgmts:\\.\root\cimv2")
    Set systemInfo = wmi.ExecQuery("Select * From Win32_ComputerSystem")
    
    for each propertyName in systemInfo
        computerName = propertyName.Name
        exit for
    next 
   
   GetLocalComputerName = computerName
End Function
 
 
Function GetRunningBizTalkHosts(machineName) 
 
    dim hosts
     
    Set hosts = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery("SELECT * FROM MSBTS_HostInstance WHERE RunningServer = '" & machineName & "' AND ServiceState = 4")
    
    Set GetRunningBizTalkHosts = hosts
 
end function
 
 



No comments:

Post a Comment