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
'#Include File:SQL2008Constants.vbs
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
'#Include File:ConnectionString.vbs
Function BuildConnectionString(strServer, strDatabase)
ON ERROR RESUME NEXT
Err.Clear
Dim dataSource
dataSource = BuildServerName(strServer, "")
BuildConnectionString = "Data Source=" & EscapeConnStringValue(dataSource) & ";Initial Catalog=" & EscapeConnStringValue(strDatabase) & ";Integrated Security=SSPI"
End Function
Function BuildConnectionStringWithPort(ByVal strServer, ByVal strDatabase, ByVal tcpPort)
ON ERROR RESUME NEXT
Err.Clear
Dim dataSource
dataSource = strServer
If ((tcpPort <> "0") And (tcpPort <> "")) Then
dataSource = dataSource & "," & tcpPort
End If
BuildConnectionStringWithPort = "Data Source=" & EscapeConnStringValue(dataSource) & ";Initial Catalog=" & EscapeConnStringValue(strDatabase) & ";Integrated Security=SSPI"
End Function
' 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 EscapeWQLString (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear
EscapeWQLString = Replace(strValue, "'", "\'")
End Function
Function GetTcpPort (ByVal strServer)
ON ERROR RESUME NEXT
Err.Clear
Dim tcpPort
tcpPort = ""
Call BuildServerName(strServer, tcpPort)
GetTcpPort = tcpPort
End Function
Function BuildServerName(ByVal strServer, ByRef tcp)
ON ERROR RESUME NEXT
Err.Clear
Dim pathArray, instanceName, computerName, ip, serverName
Dim oWMI, oQuery
ip= ""
pathArray = Split(strServer, "\")
computerName = pathArray(0)
instanceName = "MSSQLSERVER"
if (pathArray.Count > 1) Then
instanceName = pathArray(1)
End If
serverName = strServer
Set oWMI = GetObject("winmgmts:\\" & computerName & "\root\Microsoft\SqlServer\" & SQL_WMI_NAMESPACE)
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND PropertyName = 'ListenOnAllIPs'")
If oQuery.Count >0 Then
Dim isListenAll
Set isListenAll = oQuery.ItemIndex(0)
If(isListenAll.PropertyNumVal = 1) Then
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = 'IPAll' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
tcp = oQuery.ItemIndex(0).PropertyStrVal
If ((tcp <> "0") And (tcp <> "")) Then
serverName = serverName & "," & tcp
Else tcp = ""
End If
End If
Else
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName <> '' AND PropertyName = 'Enabled' AND PropertyNumVal = 1")
If (oQuery.Count > 0) Then
Dim ipAddressName
ipAddressName = oQuery.ItemIndex(0).IPAddressName
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = '"& EscapeWQLString(ipAddressName) &"' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
tcp = oQuery.ItemIndex(0).PropertyStrVal
End If
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = '"& EscapeWQLString(ipAddressName) &"' AND PropertyName = 'IpAddress' AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
ip = oQuery.ItemIndex(0).PropertyStrVal
End If
If ip <> "" Then
serverName = ip
End If
If ((tcp <> "0") And (tcp <> "")) Then
serverName = servername & "," & tcp
Else tcp = ""
End If
End If
End If
End If
On Error Goto 0
BuildServerName = serverName
End Function'#Include File: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
e.Clear
On Error Resume Next
cnADOConnection.Open strProv
e.Save
'get fresh tcp port and try to connect again
if 0 <> Err.number then
e.Clear
Err.Clear
strProv = BuildConnectionString(sSQLConnectionString, "master")
cnADOConnection.Open strProv
e.Save
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
end if
On Error Goto 0
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>