Paul Gu | Blog

sharing is caring

Get list of BizTalk host instance with specific status

The code below expects to execute on the machine its querying for.

Code:

'
' WMI script to get list of BizTalk host instance with specific status
' 
' MSBTS_HostInstance.ServiceState valid values
'
'Stopped:	          1
'Start pending:    2
'Stop pending:     3
'Running:	          4
'Continue pending: 5
'Pause pending:    6
'Paused:           7
'Unknown:          8
'

Option Explicit

Main

Sub Main

	'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 <> 1) Then
		PrintUsage()
		wscript.quit 0
	End If

	Dim statusID

	statusID = Args(0)


    dim hosts
    dim host
    dim computerName
    computerName = GetLocalComputerName()

   ' returns a wmi object set of MSBTS_HostInstance objects
   '
    Set hosts = GetRunningBizTalkHosts( computerName, statusID )
    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, sID) 
    dim hosts
    Set hosts = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery("SELECT * FROM MSBTS_HostInstance WHERE RunningServer = '" & machineName & "' AND ServiceState = " & sID)
    Set GetRunningBizTalkHosts = hosts
end Function

Sub PrintUsage()
	WScript.Echo "Usage:" + Chr(10) + Chr(10) + _
		"  cscript ListRunningBizTalkHost.vbs " + _
		Chr(10) + Chr(10) + _
		"    Where: " + _
		Chr(10) + Chr(10) + _
		"     is one of following" + Chr(10) + _
		Chr(10) + Chr(10) + _
		"    Stopped:           1 " + Chr(10) + _
		"    Start pending:     2 " + Chr(10) + _
		"    Stop pending:      3 " + Chr(10) + _
		"    Running:           4 " + Chr(10) + _
		"    Continue pending:  5 " + Chr(10) + _
		"    Pause pending:     6 " + Chr(10) + _
		"    Paused:            7 " + Chr(10) + _
		"    Unknown:           8 " + Chr(10)
End Sub

Next Post

Previous Post

Leave a Reply

© 2024 Paul Gu | Blog

Theme by Anders Norén