SQL Agent Job Discovery

Microsoft.SQLServer.2008.AgentJobDiscovery (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsMicrosoft.SQLServer.SQLDiscoveryAccount
OutputTypeSystem.Discovery.Data

Member Modules:

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

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$
SyncTimestring$Config/SyncTime$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<DataSourceModuleType ID="Microsoft.SQLServer.2008.AgentJobDiscovery" Accessibility="Internal" RunAs="SQL!Microsoft.SQLServer.SQLDiscoveryAccount">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:integer"/>
<xsd:element name="SyncTime" type="xsd:string"/>
<xsd:element name="ComputerID" type="xsd:string"/>
<xsd:element name="ComputerName" type="xsd:string"/>
<xsd:element name="SQLConnectionString" type="xsd:string"/>
<xsd:element name="SQLInstanceName" type="xsd:string"/>
<xsd:element name="ServiceName" type="xsd:string"/>
<xsd:element name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
<OverrideableParameter ID="SyncTime" ParameterType="string" Selector="$Config/SyncTime$"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime>$Config/SyncTime$</SyncTime>
<ScriptName>SQLAgentJobDiscovery.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Config/ComputerID$ $Config/ComputerName$ $Config/SQLConnectionString$ $Config/SQLInstanceName$ $Config/ServiceName$"</Arguments>
<ScriptBody><Script>

'Copyright (c) Microsoft Corporation. All rights reserved.
' Parameters that should be passed to this script
' 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 ID
' 3 Computer FQDN
' 4 SQL Connection String for the instance that the Jobs are being discovered on
' 5 SQL Instance that this rule is being run for
' 6 SQL Agent Service Name that this rule is being run for

Option Explicit
SetLocale("en-us")

Const SQL_DISCOVERY_CONNECT_FAILURE = -1
Const SQL_DISCOVERY_QUERY_FAILURE = -2
Const SQL_DISCOVERY_SUCCESS = 0
Const ERR_CANNOT_INSERT_NULL = 515


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

Dim SourceID, ManagedEntityId, TargetComputer, InstanceName, ConnectionString, TargetComputerID, ServiceName

SourceId = oArgs(0)
ManagedEntityId = oArgs(1)
TargetComputerID = oArgs(2)
TargetComputer = oArgs(3)
ConnectionString = oArgs(4)
InstanceName = oArgs(5)
ServiceName = oArgs(6)

Dim oAPI, oSQLDiscoveryData

Set oAPI = MOMCreateObject("MOM.ScriptAPI")


set oSQLDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

If DoJobDiscovery(ServiceName, InstanceName, ConnectionString, oSQLDiscoveryData) &gt;= 0 Then Call oAPI.Return(oSQLDiscoveryData)
WScript.Quit()

Function DoJobDiscovery(ByVal sServiceName, ByVal sSqlInstance, ByVal sSQLConnectionString, ByVal oDisc)

Dim e
Set e = New Error

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

Dim strProv
strProv = "Server=" &amp; sSQLConnectionString &amp; ";Database=msdb;Integrated Security=SSPI"

e.Clear
On Error Resume Next
cnADOConnection.Open strProv
e.Save
On Error Goto 0
if 0 &lt;&gt; Err.number then
'Error event in here
'g_oSQL.CreateConnectionFailureAlert sInstance, Err.number, Err.Description
DoDatabaseDiscovery = SQL_DISCOVERY_CONNECT_FAILURE
Exit Function
end if

Dim oResults
e.Clear
On Error Resume Next
Set oResults = cnADOConnection.Execute("sp_help_job")
e.Save
On Error Goto 0


If e.Number &lt;&gt; 0 Then
DoJobDiscovery = SQL_DISCOVERY_QUERY_FAILURE
Exit Function
End If

Dim oSQLAgentJobInstance
Dim sJobId, sJobName, sJobOrigin, sJobDescription, sJobCategory, sJobOwner

Do While Not oResults.EOF
sJobId = oResults(0)
sJobOrigin = oResults(1)
sJobName = oResults(2)
sJobDescription = oResults(4)
sJobCategory = oResults(6)
sJobOwner = oResults(7)

Set oSQLAgentJobInstance = oSQLDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.SQLServer.2008.AgentJob']$")
' Set basic Agent Job properties
With oSQLAgentJobInstance
.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputerID
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.ServerRole']/InstanceName$", sSqlInstance
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.Agent']/ServiceName$", sServiceName
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/JobID$", sJobId
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/OriginatingServer$", sJobOrigin
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/Name$", sJobName
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/IsEnabled$", CBool(oResults(3))
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/Description$", sJobDescription
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/Category$", sJobCategory
.AddProperty "$MPElement[Name='SQL!Microsoft.SQLServer.AgentJob']/Owner$", sJobOwner
.AddProperty "$MPElement[Name='System!System.Entity']/DisplayName$", sJobName
End With


' Add Agent Job to the Instance List and move to next Agent Job
call oSQLDiscoveryData.AddInstance(oSQLAgentJobInstance)
oResults.MoveNext

Loop

Set oResults = nothing
cnADOConnection.Close
DoJobDiscovery = SQL_DISCOVERY_SUCCESS

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



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 &lt;&gt; 0 Then ThrowScriptError "Unable to create automation object '" &amp; sProgramId &amp; "'", oError
End Function

'******************************************************************************
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
On Error Resume Next
Dim oAPITemp
Set oAPITemp = MOMCreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent "SQLAgentJobDiscovery.vbs", 4001, 1, sMessage &amp; ". " &amp; oErr.m_sDescription
End Function

'******************************************************************************
Function ThrowScriptError(Byval sMessage, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function
'******************************************************************************


</Script></ScriptBody>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.Discovery.Data</OutputType>
</DataSourceModuleType>