BizTalk Application Service Discovery Module

Microsoft.BizTalk.Server.2016.BizTalkApplicationServiceDiscovery (DataSourceModuleType)

This module discovers all BizTalk application service (BTSNTSvc.exe) instances installed on different run-time computers in a BizTalk group.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsMicrosoft.BizTalk.DiscoveryAccount
OutputTypeSystem.Discovery.Data

Member Modules:

ID Module Type TypeId RunAs 
DataSource DataSource Microsoft.Windows.TimedScript.DiscoveryProvider Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval SecondsThis is the interval (in seconds) at which the script associated with the module is run.
TimeoutSecondsint$Config/TimeoutSeconds$Timeout SecondsThis is the timeout (in seconds) after which execution of the script associated with the module is terminated if not yet completed.

Source Code:

<DataSourceModuleType ID="Microsoft.BizTalk.Server.2016.BizTalkApplicationServiceDiscovery" Accessibility="Public" RunAs="Microsoft.BizTalk.DiscoveryAccount" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DataSource" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime/>
<ScriptName>Microsoft.BizTalk.Server.2016.BizTalkApplicationServiceDiscovery.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.ServerRole"]/ComputerName$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.ServerRole"]/MgmtDbServerName$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.ServerRole"]/MgmtDbName$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.BizTalkRuntimeRole"]/NetbiosComputerName$</Arguments>
<ScriptBody><Script>
'Copyright (c) Microsoft Corporation. All rights reserved

'This script discovers all BizTalk application services on a computer
'along with its hosting relations with runtime role.

Option Explicit

Const APPSVC_DISCOVERY_CONNECT_FAILURE = -1
Const APPSVC_DISCOVERY_QUERY_FAILURE = -2
Const HKEY_LOCAL_MACHINE = &amp;H80000002

Dim oArgs
Set oArgs = WScript.Arguments
if oArgs.Count &lt; 6 Then
Wscript.Quit -1
End If

Dim SourceID, ManagedEntityId, TargetComputer, DataBaseServer, DataBaseName, NetbiosComputerName

SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
TargetComputer = oArgs(2)
DataBaseServer = oArgs(3)
DataBaseName = oArgs(4)
NetbiosComputerName = oArgs(5)

Call GetBizTalkApplicationServices()

Sub GetBizTalkApplicationServices()
Dim defaultConnStr
Dim objAPI, objDiscoveryData

defaultConnStr = "Server=" &amp; DataBaseServer &amp; ";Database=" &amp; DataBaseName &amp; ";Trusted_Connection=yes"
Set objAPI = CreateObject("MOM.ScriptAPI")
Set objDiscoveryData = objAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

CreateObjectsAndRelations defaultConnStr, objAPI, objDiscoveryData
Call objAPI.Return(objDiscoveryData)
End Sub

Function GetRegistryKeyValue(strKey, strValue)
Dim sMethod, hTree
Dim oRegistry, oMethod, oInParam, oOutParam
sMethod = "GetStringValue"
hTree = HKEY_LOCAL_MACHINE

Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" &amp; _
TargetComputer &amp; "/root/default:StdRegProv")

Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()

oInParam.hDefKey = hTree
oInParam.sSubKeyName = strKey
oInParam.sValueName = strValue

Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
GetRegistryKeyValue = oOutParam.Properties_("sValue")
if IsNull(GetRegistryKeyValue) then
GetRegistryKeyValue = ""
end if
End Function

Function GetValueByOpc(strSource, strOpc)
Dim objRegEx, Match, Matches, StrReturnStr
Set objRegEx = New RegExp
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = """[^ ][^""]+"""
Set Matches = objRegEx.Execute(strSource)

Select Case strOpc
Case "1"
GetValueByOpc = "BTSSvc$" &amp; Replace(Matches(2).value,"""","")
Case "2"
GetValueByOpc = Replace(Matches(2).value,"""","")
Case "3"
GetValueByOpc = Replace(Matches(1).value,"""","")
Case "4"
GetValueByOpc = Replace(Matches(0).value,"""","")
End Select
End Function

Function CreateObjectsAndRelations(defaultConnStr, byRef objAPI, byRef objDiscoveryData)
Dim ObjError
Set ObjError = New Error

Dim cnADOConnection
Set cnADOConnection = MomCreateObject("ADODB.Connection")
cnADOConnection.Provider = "SQLNCLI11"
cnADOConnection.ConnectionTimeout = 15

ObjError.Clear
On Error Resume Next
cnADOConnection.Open defaultConnStr
ObjError.Save
On Error Goto 0
If 0 &lt;&gt; Err.number then
CreateObjectsAndRelations = APPSVC_DISCOVERY_CONNECT_FAILURE
Exit Function
End If

Dim strSQLQuery
Dim objResults

ObjError.Clear
On Error Resume Next
strSQLQuery = "select " &amp; _
"svr.Name, hs.Name, hi.InstallationContext " &amp; _
"from adm_Server svr with (NoLock) " &amp; _
"inner join adm_Server2HostMapping s2h with (NoLock) on svr.Id = s2h.ServerId " &amp; _
"inner join adm_HostInstance hi with (NoLock) on s2h.Id = hi.Svr2HostMappingId " &amp; _
"inner join adm_Host hs with (NoLock) on s2h.HostId = hs.Id " &amp; _
"where hs.HostType = 1 and svr.Name = '" &amp; NetbiosComputerName &amp; "'"
Set objResults = cnADOConnection.Execute(strSQLQuery)
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
CreateObjectsAndRelations = APPSVC_DISCOVERY_QUERY_FAILURE
If (objResults &lt;&gt; null) Then objResults.Close
Exit Function
End If

Dim objSourceInst
Dim objTargetInst
Dim objHost, objGroup
Dim regKey, regValue

Set objSourceInst = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkRuntimeRole']$")
call objSourceInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.ServerRole']/ComputerName$", TargetComputer)
call objSourceInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputer)

Do While Not objResults.EOF
regKey = "SYSTEM\CurrentControlSet\Services\" &amp; GetValueByOpc(CStr(objResults(2)), "1")
regValue = GetRegistryKeyValue(regKey, "DisplayName")
If Not(IsNull(regValue) or IsEmpty(regValue) or regValue = "") Then
Set objTargetInst = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']$")
call objTargetInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputer)
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.ServerRole']/ComputerName$", TargetComputer)
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']/Name$", GetValueByOpc(CStr(objResults(2)), "1"))
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']/ImagePath$", CStr(objResults(2)))
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']/BizTalkHostName$", CStr(objResults(1)))
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']/BizTalkGroupName$", GetValueByOpc(CStr(objResults(2)), "3"))
call objTargetInst.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkApplicationService']/InstallPath$", GetValueByOpc(CStr(objResults(2)), "4"))
call objDiscoveryData.AddInstance(objTargetInst)

CreateRelationShip objDiscoveryData, objSourceInst, objTargetInst, "$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkRuntimeHostsApplicationService']$"

Set objHost = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkHostDeployment']$")
call objHost.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkHostDeployment']/Name$", CStr(objResults(1)))
call objDiscoveryData.AddInstance(objHost)

CreateRelationShip objDiscoveryData, objHost, objTargetInst, "$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkHostDeploymentContainsApplicationService']$"

Set objGroup = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkGroupDeployment']$")
call objGroup.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkGroupDeployment']/MgmtDbName$", DataBaseName)
call objGroup.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkGroupDeployment']/MgmtDbServerName$", DataBaseServer)

CreateRelationShip objDiscoveryData, objGroup, objHost, "$MPElement[Name='Microsoft.BizTalk.Server.2016.BizTalkGroupDeploymentContainsHost']$"
End If

objResults.MoveNext
Loop

cnADOConnection.Close
End Function

Function CreateRelationShip(byRef objDiscoveryData, SourceObject, TargetObject, strRelationShipClassId)
Dim objRelation
Set objRelation = objDiscoveryData.CreateRelationshipInstance(strRelationShipClassId)
objRelation.Source = SourceObject
objRelation.Target = TargetObject
objDiscoveryData.AddInstance objRelation
End Function

Sub CreateEvent(byRef objAPI, lEventID, lEventType, strMessage)
objAPI.LogScriptEvent "Test", lEventID, lEventType, strMessage
End Sub

Function MomCreateObject(ByVal sProgramId)
Dim ObjError
Set ObjError = New Error

On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then WScript.Quit
End Function

Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DataSource"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>