Dim aux1
Dim oAPI, oArgs, oDiscoveryData, sMessage
Dim SourceId, ManagedEntityId, sComputer, bIsVirtual, bLogDetail
Dim oWMI, oWMIcluster, cServices, cClusterResources, oClusterResource
Dim sResourceName, wshNetwork
aux1 = 0
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oArgs = WScript.Arguments
If oArgs.Count < 4 Then Call ShowUsage()
SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
sComputer = oArgs(2)
If Len(oArgs(3)) > 0 Then
bIsVirtual = CBool(oArgs(3))
Else
bIsVirtual = False
End If
If Len(oArgs(4)) > 0 Then
bLogDetail = CBool(oArgs(4))
Else
bLogDetail = False
End If
Set wshNetwork = CreateObject("Wscript.Network")
sMessage = "Discovery started with following parameters: " & VbCrLf & _
"Source ID: " & SourceId & VbCrLf & _
"Managed Entity ID: " & ManagedEntityId & VbCrLf & _
"Computer: " & sComputer & VbCrLf & _
"Network: " & wshNetwork.ComputerName & VbCrLf & _
"Discover bIsVirtual : " & bIsVirtual & VbCrLf & _
"User credentials: " & wshNetwork.UserDomain & "\" & wshNetwork.UserName
Call LogDetail(EVENTNO_SCRIPT_STARTED,sMessage)
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
On Error Resume Next
Set oWMI = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set cServices = oWMI.ExecQuery("select * from win32_service where name = 'MSDTC'")
If Err <> 0 Then Call ThrowScriptError(EVENTNO_ERROR_WMI,EVENT_LEVEL_ERROR,"Error attempting to locate MSDTC service through WMI.",True)
On Error GoTo 0
If bIsVirtual = True Then
Call LogDetail(EVENTNO_EMPTY, "Start clusted discovery")
Set oWMIcluster = GetObject("winmgmts:\\" & sComputer & "\root\mscluster")
Set cClusterResources = oWMIcluster.ExecQuery("select * from MSCluster_Resource where type like 'Distributed Transaction Coordinator%'")
If Err <> 0 Then Call ThrowScriptError(EVENTNO_ERROR_WMI,EVENT_LEVEL_ERROR,"Error attempting to locate MSDTC service through WMI.",True)
On Error GoTo 0
'Check for clustered instances
For Each oClusterResource In cClusterResources
Dim roleName
Dim cResources, oResource, sNameResource, cGroups, oGroup
Set cGroups = oWMIcluster.ExecQuery("ASSOCIATORS OF {mscluster_Resource='" & oClusterResource.Name & "'} where AssocClass = MSCluster_ResourceGroupToResource Role=PartComponent")
If Err <> 0 Then Call ThrowScriptError(EVENTNO_ERROR_WMI,EVENT_LEVEL_ERROR,"Error attempting to locate MSDTC service through WMI.",True)
On Error GoTo 0
For Each oGroup In cGroups
roleName = oGroup.Name
Next
Call LogDetail(EVENTNO_SERVER_FOUND,"Clustered MSDTC role " & oClusterResource.Name & " found on " & sComputer & " .")
Call CreateServiceInstance("MSDTCClusteredRole",oClusterResource.Name, roleName)
Next
If aux1 = 0 Then
Call LogDetail(EVENTNO_EMPTY,"No clustered MSDTC service found!")
End If
Else
'Check for regular instances
If cServices.Count > 0 Then
Call LogDetail(EVENTNO_SERVER_FOUND,"Local MSDTC instance found on " & sComputer & " .")
Call CreateServiceInstance("MSDTCService","MSDTC", "MSDTC")
else
Call LogDetail(EVENTNO_EMPTY,"No local MSDTC service found!")
End If
End If
oAPI.Return(oDiscoveryData)
Call LogDetail(EVENTNO_DISCDATA_SUBMITTED,"Discovery data submitted.")
Call LogDetail(EVENTNO_SCRIPT_ENDED,"MSDTC Server Discovery script ended.")
'Create discovery data
Sub CreateServiceInstance(ServiceType,Name,DisplayName)
Dim oServices, oInstance
Select Case (ServiceType)
Case "MSDTCService"
Set oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.MSDTC.10.0.Servers']$")
Call oInstance.AddProperty("$MPElement[Name='Microsoft.MSDTC.10.0.ServerRole']/ServiceName$", Name)
Case "MSDTCClusteredRole"
Set oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.MSDTC.10.0.ClusteredRoles']$")
Call oInstance.AddProperty("$MPElement[Name='Microsoft.MSDTC.10.0.ClusteredRoles']/RoleName$", Name)
End Select
Call oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", sComputer)
Call oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", DisplayName)
Call oDiscoveryData.AddInstance(oInstance)
End Sub
'Log event information
Sub LogDetail(EventNo,Message)
Message = VbCrLf & "Calling MP Element ID: $MPElement$" & vbcrlf & Message
If bLogDetail = True Then
Call oAPI.LogScriptEvent(SCRIPT_NAME,EventNo,EVENT_LEVEL_INFO,Message)
End If
End Sub
'Log event and quit the script if Abort is set to true
Sub ThrowScriptError(EventNo,Severity,Message,Abort)
Message = VbCrLf & Message
Call oAPI.LogScriptEvent(SCRIPT_NAME,EventNo,Severity,Message)
If Abort = True Then WScript.Quit
End Sub