Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error GoTo 0
If oError.Number <> 0 Then ThrowScriptError "Unable to create automation object '" & sProgramId & "'", oError
End Function
' ##### Scripts\SQL2012Constants.vbs
Const SQL_VERSION = "2012"
Const SQL_VERSION_NUMBER = "11"
Const SQL_WMI_NAMESPACE = "ComputerManagement11"
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
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
On Error Resume Next
Dim oAPITemp
Set oAPITemp = MOMCreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent WScript.ScriptName, 4001, 1, sMessage & ". " & oErr.Description
End Function
Function ThrowScriptError(Byval sMessage, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function
Sub HandleError(customMessage)
Dim localLogger
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Wscript.Quit 0
End If
End Sub
Function HandleErrorContinue(customMessage)
Dim localLogger
HandleErrorContinue = False
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Err.Clear
HandleErrorContinue = True
End If
End Function
' ##### ..\Scripts\Common\Discovery\SQLReplicationComponentsDiscovery.vbs
'Copyright (c) Microsoft Corporation. All rights reserved.
' Parameters that should be passed to this script
' 0 MPElement ID ($MPElement$)
' 1 Target Id for ME this rule is running against ($Target/Id$)
' 2 Computer (FQDN) that SQL instance is hosted on
' 3 SQL Connection String for the instance that the DBs are being discovered on
' 4 SQL Instance that this rule is being run for
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set oSQLDiscoveryData = oAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId)
If DoReplicationDiscovery(TargetComputerID, InstanceName, ConnectionString, oSQLDiscoveryData) >= 0 Then Call oAPI.Return(oSQLDiscoveryData)
' This function should be used to escape Connection String keywords.
Function EscapeConnStringValue (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear
EscapeConnStringValue = "{" + Replace(strValue, "}", "}}") + "}"
End Function
Function DoReplicationDiscovery(ByVal sTargetComputerID, ByVal sSqlInstance, ByVal sSQLConnectionString, ByVal oSQLDiscoveryData)
Dim e
Dim oObjectInstance
Dim cnADOConnection
Dim cnADOConnection2
Dim strProv
Dim oResults
Dim oResults2
Dim oRel
Dim oDatabase
Set e = New Error
Set cnADOConnection = MomCreateObject("ADODB.Connection")
cnADOConnection.Provider = "sqloledb"
cnADOConnection.ConnectionTimeout = 30
strProv = "Server=" & EscapeConnStringValue(sSQLConnectionString) & ";Database=master;Trusted_Connection=yes"
e.Clear
On Error Resume Next
cnADOConnection.Open strProv
e.Save
On Error Goto 0
if 0 <> Err.number then
'Error event in here
'g_oSQL.CreateConnectionFailureAlert sInstance, Err.number, Err.Description
DoReplicationDiscovery = SQL_DISCOVERY_CONNECT_FAILURE
Exit Function
end if
e.Clear
On Error Resume Next
Set oResults = cnADOConnection.Execute("sp_helpdistpublisher")
e.Save
On Error Goto 0
if 0 <> e.number then
'Error event in here
DoReplicationDiscovery = SQL_DISCOVERY_QUERY_FAILURE
Exit Function
end if
' Create the distributor object if it exists on this instance
' Remote distributors can be discovered from publishers but this is ignored for now
If oResults.Fields.Count > 0 Then
Set oObjectInstance = oSQLDiscoveryData.CreateClassInstance(SQL_REPLICATION_DISTRIBUTOR_CLASS)
With oObjectInstance
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerID
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.ServerRole']/InstanceName$", sSqlInstance
' TODO: Remove. Useless property - we already have all this information through hosting relationships.
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Distributor']/DistributionServer$", BuildServerName(TargetComputerID, sSqlInstance)
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", BuildServerName(TargetComputerID, sSqlInstance)
End With
' Try extract additional properties if publisher is on the same instance as distributor.
' TODO: This properties should not be discovered for distributor, they're publisher's view on the distributor.
e.Clear
On Error Resume Next
Set oResults = cnADOConnection.Execute("sp_get_distributor")
e.Save
On Error Goto 0
If 0 <> e.Number Then
'Error event in here
DoReplicationDiscovery = SQL_DISCOVERY_QUERY_FAILURE
Exit Function
End If
If oResults(1) = True Then
With oObjectInstance
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Distributor']/DistributionDBInstalled$", CBool(oResults(2))
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Distributor']/IsDistributionPublisher$", CBool(oResults(3))
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Distributor']/HasRemoteDistributionPublisher$", CBool(oResults(4))
End With
End If
' 'sp_helpdistributor' should be executed on the publisher but not the distributor.
' All this properties are valid for particular publisher but can vary on distributor.
' For example, distributor can have multiple distribution databases.
'
'e.Clear
'On Error Resume Next
' Set oResults = cnADOConnection.Execute("sp_helpdistributor")
' e.Save
'On Error Goto 0
On Error Resume Next
Set oResults2 = cnADOConnection.Execute("select name from sys.databases where is_distributor = 1")
e.Save
On Error Goto 0
If e.Number = 0 And oResults2.Fields.Count > 0 Then
Do While Not oResults2.EOF
Set oRel = oSQLDiscoveryData.CreateRelationshipInstance(SQL_DISTRIBUTOR_CONTAINS_DATABASE_RELATIONSHIP)
Set oDatabase = oSQLDiscoveryData.CreateClassInstance(SQL_DATABASE_CLASS)
With oDatabase
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerID
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.ServerRole']/InstanceName$", sSqlInstance
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Database']/DatabaseName$", oResults2(0)
End With
Call oSQLDiscoveryData.AddInstance(oDatabase)
oRel.Source = oObjectInstance
oRel.Target = oDatabase
Call oSQLDiscoveryData.AddInstance(oRel)
oResults2.MoveNext
Loop
End If
' Get distributor information from publisher side
' TODO: Looks like it discovers distributor as a publisher. Need more strict condition to determine publishers.
e.Clear
On Error Resume Next
Set oResults = cnADOConnection.Execute("sp_helpdistributor")
e.Save
On Error Goto 0
If e.Number = 0 And oResults.Fields.Count > 0 And Not IsNull(oResults(0)) Then
' Publisher is created in case of any results returned
Set oObjectInstance = oSQLDiscoveryData.CreateClassInstance(SQL_REPLICATION_PUBLISHER_CLASS)
Dim sPublisherName : sPublisherName = BuildServerName(TargetComputerID, sSqlInstance)
With oObjectInstance
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerID
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.ServerRole']/InstanceName$", sSqlInstance
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Publisher']/Name$", sPublisherName
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Publisher']/DistributionDB$", oResults(1)
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Publisher']/PublisherType$", oResults(11)
' TODO: Remove. Useless property - this is publisher SQL Server instance name.
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Publisher']/DataSource$", sPublisherName
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Publisher']/WorkingDirectory$", oResults(2)
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", sPublisherName
End With
Call oSQLDiscoveryData.AddInstance(oObjectInstance)
End If
' Get subscriptions on subscriber side
' TODO: Review low-priviledge required for this to work.
'e.Clear
'On Error Resume Next
' Set oResults = cnADOConnection.Execute("exec sp_MSenumallsubscriptions @subscription_type = N'both'")
' e.Save
'On Error Goto 0
'If e.Number = 0 And Not oResults.BOF And Not oResults.EOF Then
' Subscriber is created in case of any results returned
' Set oObjectInstance = oSQLDiscoveryData.CreateClassInstance(SQL_REPLICATION_SUBSCRIBER_CLASS)
' Dim sSubscriberName : sSubscriberName = BuildServerName(TargetComputerID, sSqlInstance)
' With oObjectInstance
' .AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerID
' .AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.ServerRole']/InstanceName$", sSqlInstance
' .AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", sSubscriberName
' End With
' Call oSQLDiscoveryData.AddInstance(oObjectInstance)
'End If
' Simple function to build SQL Server connection string
Function BuildServerName(ByVal sComputerName, ByVal sInstanceName)
If sInstanceName = "MSSQLSERVER" Then
BuildServerName = sComputerName
Else
BuildServerName = sComputerName & "\" & sInstanceName
End If
End Function </Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>