Use the following code to monitor and delete HWS orchestration instances that finished without consuming the Hws_Finish message (http://schemas.microsoft.com/Hws/2003/Hws_Finish#HwsMessage). Such instances are likely to occur in dependent composition scenarios under stress loads.
' This script needs to be run under a user account that is a member of the BizTalk Administrators
' group. This script needs to be run on a machine that is configured with BizTalk administration
' tools.
'
'Impersonate the user account running the script to connect to BizTalk WMI objects.
'
Set objWMIServices = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}\\.\root\MicrosoftBizTalkServer")
'
'Create the event sink object that receives the service instance suspended events.
'
Set sinkSuspInst = WScript.CreateObject("WbemScripting.SWbemSink","SINK_INST_")
objWMIServices.ExecNotificationQueryAsync sinkSuspInst, "select * from MSBTS_ServiceInstanceSuspendedEvent"
'
'Wait for the events to fire.
'
while(true)
WScript.Echo "Waiting for suspended service event..."
WScript.Sleep(1000)
wend
'
'Event handler subroutine for suspendend service event.
'
Sub SINK_INST_OnObjectReady(objObject, objAsyncContext)
On Error Resume Next
Dim unconsumedMessageInstanceSet, objWMIServices, serviceInstanceSet
'
'Impersonate the user account running the script to connect to BizTalk WMI objects.
'
Set objWMIServices = GetObject("WinMgmts:{impersonationLevel=impersonate, (security)}\\.\root\MicrosoftBizTalkServer")
If Err <> 0Then
PrintWMIErrorThenExit Err.Description, Err.Number
End If
WScript.Echo ("")
WScript.Echo ("Entering the event handler for suspendend service instances.")
WScript.Echo ("Service instance id: " & objObject.InstanceID)
Wscript.Echo ("Service status: " & objObject.ServiceStatus)
'
'Check if the service completed with unconsumed messages, status code 4.
'If not exit the event handler subroutine.
'
if objObject.ServiceStatus <> 4 then
WScript.Echo ("The service instance was not in the Compelted with un-consumed messages state.")
WScript.Echo ("Service instance not terminated.")
Exit Sub
end if
'
'Search for message instances that were not consumed by the service instance.
'
WScript.Echo("Searching for message instances not consumed by the service instance.")
Query = "Select * from MSBTS_MessageInstance where ReferenceType = 8 and ServiceInstanceID =""" & objObject.InstanceID & """"
WScript.Echo (Query)
Set unconsumedMessageInstanceSet = objWMIServices.ExecQuery(Query)
If Err <> 0Then
WScript.Echo ("Query failed.")
PrintWMIErrorThenExit Err.Description, Err.Number
Exit Sub
End If
'
'Exit the event handler subroutine if there were no unconsumed messages.
'
if (unconsumedMessageInstanceSet.Count = 0) then
WScript.Echo ("No unconsumed messages.")
WScript.Echo ("Service instance not terminated.")
Exit Sub
end if
'
'Exit the event handler subroutine if there were more than one unconsumed messages.
'We do not want to terminate service instances that did not consume message instances
'other than Hws_Finish.
'
if (unconsumedMessageInstanceSet.Count > 1) then
WScript.Echo ("The unconsumed message count was more than one.")
WScript.Echo ("Service instance not terminated.")
Exit Sub
end if
'
'Loop over the set of unconsumed messages.
'The set should contain exactly one message instance at this point.
'
For each messageInstance in unconsumedMessageInstanceSet
'
'If the message type is not Hws_Finish then exit subroutine.
'
If (messageInstance.MessageType <> "http://schemas.microsoft.com/Hws/2003/Hws_Finish#HwsMessage") then
WScript.Echo ("The unconsumed message was not the Hws Finish message.")
WScript.Echo ("Service instance not terminated.")
Exit Sub
end if
'
'Search for the service instance that needs to be terminated.
'
WScript.Echo ("Querying for the service instance to be terminated.")
Query = "Select * from MSBTS_ServiceInstance where InstanceID = """ & objObject.InstanceID& """"
WScript.Echo (Query)
Set serviceInstanceSet = objWMIServices.ExecQuery(Query)
If Err <> 0 Then
PrintWMIErrorThenExit Err.Description, Err.Number
Exit Sub
End If
'
'Loop over the service instances found.
'The set should have exactly one service instance.
'Call Terminate method on the service instance.
'
For each serviceInstance in serviceInstanceSet
WScript.Echo ("Terminating instance "+ serviceInstance.InstanceID)
serviceInstance.Terminate()
If Err <> 0 Then
PrintWMIErrorThenExit Err.Description, Err.Number
else
WScript.Echo "Service instance terminated successfully."
End If
Next
Next
End Sub
'
'Subroutine for displaying WMI errors.
'
Sub PrintWMIErrorThenExit(strErrDesc, ErrNum)
On Error Resume Next
DimobjWMIError
Set objWMIError = CreateObject("WbemScripting.SwbemLastError")
If (TypeName(objWMIError) = "Empty") Then
wscript.echo strErrDesc & " (HRESULT: "& Hex(ErrNum) & ")."
Else
wscript.echo objWMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."
Set objWMIError= nothing
EndIf
End Sub
Reference
MSBTS_ServiceInstance (WMI)
Other Resources
WMI Script Samples