BAM Run-Time Component Discovery Module

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

This module discovers BAM run-time components that are installed on a computer and their relationship to a specific 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.
ComputerNamestring$Config/ComputerName$Computer NameThe discovery is successful only if the name of the computer where the script is running matches this value.

Source Code:

<DataSourceModuleType ID="Microsoft.BizTalk.Server.2016.BAMRuntimeDiscovery" Accessibility="Public" RunAs="Microsoft.BizTalk.DiscoveryAccount" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element name="ComputerName" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="ComputerName" Selector="$Config/ComputerName$" ParameterType="string"/>
</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.BAMRuntimeDiscovery.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.ServerRole"]/ComputerName$ $Target/Property[Type="Microsoft.BizTalk.Server.2016.BizTalkRuntimeRole"]/NetbiosComputerName$ $Config/ComputerName$</Arguments>
<ScriptBody><Script>
'Copyright (c) Microsoft Corporation. All rights reserved

'This script discovers BAM runtime component on a computer passed as parameter
'$Config/ComputerName$. If computer name is not passed then it discovers BAM on
'a runtime computer with lowest server ID in the management database. This also
'discovers BAM role and the containment of BAM runtime in it.

Option Explicit

Const HKEY_LOCAL_MACHINE = &amp;H80000002
Const BAM_DISCOVERY_CONNECT_FAILURE = -1
Const BAM_DISCOVERY_QUERY_FAILURE = -2
Const MGMTDB_CONNECT_FAILURE = -3
Const MGMTDB_QUERY_FAILURE = -4

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

Dim SourceID, ManagedEntityId, TargetComputer
Dim ComputerName, NetbiosComputerName
Dim BAMDbName, BAMDbServerName

SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
ComputerName = oArgs(2)
NetbiosComputerName = oArgs(3)
TargetComputer = null
if oArgs.Count &gt; 4 Then
TargetComputer = oArgs(4)
end if

Call GetBAMRuntime()

Sub GetBAMRuntime()
Dim objAPI, objDiscoveryData
Set objAPI = CreateObject("MOM.ScriptAPI")
Set objDiscoveryData = objAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

BAMDbName = null
BAMDbServerName = null
GetBAMDatabase BAMDbName, BAMDbServerName

if Not IsNull(BAMDbName) and Not IsNull(BAMDbServerName) then
Dim defaultConnStr
defaultConnStr = "Server=" &amp; BAMDbServerName &amp; ";Database=" &amp; BAMDbName &amp; ";Trusted_Connection=yes"
CreateObjectsAndRelations defaultConnStr, objAPI, objDiscoveryData
end if

Call objAPI.Return(objDiscoveryData)
End Sub

Function GetBAMDatabase(byRef BAMDbName, byRef BAMDbServerName)
Dim MgmtDbName, MgmtDbServerName
MgmtDbName = GetRegistryKeyValue("SOFTWARE\Microsoft\BizTalk Server\3.0\Administration", "MgmtDBName")
MgmtDbServerName = GetRegistryKeyValue("SOFTWARE\Microsoft\BizTalk Server\3.0\Administration", "MgmtDBServer")

if IsNull(TargetComputer) then
Dim ObjError
Set ObjError = New Error

Dim defaultConnStr
defaultConnStr = "Server=" &amp; MgmtDbServerName &amp; ";Database=" &amp; MgmtDbName &amp; ";Trusted_Connection=yes"

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
'Error event to goin here
GetBAMDatabase = MGMTDB_CONNECT_FAILURE
Exit Function
End If

Dim objResults

ObjError.Clear
On Error Resume Next
Set objResults = cnADOConnection.Execute("select top 1 * from adm_Server srv with (NoLock) order by srv.Id asc")
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then
' Error event to go in here
GetBAMDatabase = MGMTDB_QUERY_FAILURE
If (objResults &lt;&gt; null) Then objResults.Close
Exit Function
End If

TargetComputer = CStr(objResults(1))
objResults.Close
end if

Dim DiscoverBAMRuntime
DiscoverBAMRuntime = false
if StrComp(TargetComputer, NetbiosComputerName, 1) = 0 then
DiscoverBAMRuntime = true
end if

if (DiscoverBAMRuntime) then
Dim strGroupQuery
strGroupQuery = "select * from MSBTS_GroupSetting"
Dim GroupWbemObjSet, WbemGroupObject
Set GroupWbemObjSet = GetWMICollection(ComputerName, "MicrosoftBizTalkServer", strGroupQuery)
Dim strEval
For Each WbemGroupObject In GroupWbemObjSet
strEval = Eval(IsObject(WbemGroupObject.Properties_("BamDBName")))
If (strEval &lt;&gt; "False") Then
BAMDbName = WbemGroupObject.Properties_("BamDBName").value
End If
strEval = Eval(IsObject(WbemGroupObject.Properties_("BamDBServerName")))
If (strEval &lt;&gt; "False") Then
BAMDbServerName = WbemGroupObject.Properties_("BamDBServerName").value
End If
Next
end if
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 = BAM_DISCOVERY_CONNECT_FAILURE
Exit Function
End If

Dim objResults

ObjError.Clear
On Error Resume Next
Set objResults = cnADOConnection.Execute("select * from bam_Metadata_Properties bmp with (NoLock) where bmp.Scope = 'ArchivingDatabase'")
ObjError.Save
On Error Goto 0

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

Dim oBAMRole, oBAMRuntime

Set oBAMRole = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRole']$")
call oBAMRole.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRole']/BAMPrimaryImportDbName$", BAMDbName)
call oBAMRole.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRole']/BAMPrimaryImportDbServerName$", BAMDbServerName)
call objDiscoveryData.AddInstance(oBAMRole)

Set oBAMRuntime = objDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRuntime']$")
call oBAMRuntime.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", ComputerName)
call oBAMRuntime.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRuntime']/BAMPrimaryImportDbName$", BAMDbName)
call oBAMRuntime.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRuntime']/BAMPrimaryImportDbServerName$", BAMDbServerName)
Do While Not objResults.EOF
if CStr(objResults(0)) = "ArchivingDatabase" then
if CStr(objResults(1)) = "DatabaseName" then
call oBAMRuntime.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRuntime']/BAMArchivingDbName$", CStr(objResults(2)))
else
call oBAMRuntime.AddProperty("$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMRuntime']/BAMArchivingDbServerName$", CStr(objResults(2)))
end if
end if
objResults.MoveNext
Loop
call objDiscoveryData.AddInstance(oBAMRuntime)

CreateRelationShip objDiscoveryData, oBAMRole, oBAMRuntime, "$MPElement[Name='Microsoft.BizTalk.Server.2016.BAMContainsBAMRuntime']$"

cnADOConnection.Close
End Function

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; _
ComputerName &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 GetWMICollection(TargetComputer, strWMIBaseClass, strQuery)
Dim WbemSrv, WbemObjectSet
Set WbemSrv = Getobject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; TargetComputer &amp; "\root\" &amp; strWMIBaseClass)
Set WbemObjectSet = WbemSrv.ExecQuery(strQuery)
Set GetWMICollection = WbemObjectSet
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

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>