Service Reporting Discovery Rule

Microsoft.SystemCenter.ServiceReporting.2012.ServiceReporting.DiscoveryRule (Discovery)

This discovery discovers instances of the ServiceReporting service as instances of the ServiceReporting class.

Element properties:

TargetMicrosoft.SQLServer.DBEngine
EnabledTrue
Frequency14400
RemotableFalse
CommentThis discovery discovers instances of the ServiceReporting service as instances of the ServiceReporting class.

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource Microsoft.Windows.TimedPowerShell.DiscoveryProvider Microsoft.SQLServer.SQLDiscoveryAccount

Source Code:

<Discovery ID="Microsoft.SystemCenter.ServiceReporting.2012.ServiceReporting.DiscoveryRule" Target="SQLLibrary!Microsoft.SQLServer.DBEngine" Enabled="true" ConfirmDelivery="false" Remotable="true" Priority="Normal" Comment="This discovery discovers instances of the ServiceReporting service as instances of the ServiceReporting class.">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ServiceReporting.2012.ServiceReportingService">
<Property TypeID="SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService" PropertyID="ScheduleInterval"/>
<Property TypeID="SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService" PropertyID="JobName"/>
<Property TypeID="SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService" PropertyID="ConnectionString"/>
</DiscoveryClass>
</DiscoveryTypes>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider" RunAs="SQLLibrary!Microsoft.SQLServer.SQLDiscoveryAccount" Comment="The data source for this discovery is a query to the Service Reporting database that confirms the presence of the Service Reporting feature.">
<IntervalSeconds>14400</IntervalSeconds>
<SyncTime/>
<ScriptName>ServiceReporting Discover Rule TimedPowerShell Discovery Provider Script</ScriptName>
<ScriptBody><Script>
param
(
$ConnectionString,
$CommandText
)


function Get-DateTimeFromDateAndTime ($YYYYMMDD_Date, $HHMMSS_Time)
{
$year = $YYYYMMDD_Date/10000;
$month = $YYYYMMDD_Date/100 % 100;
$day = $YYYYMMDD_Date %100;

$hour = $HHMMSS_Time/10000;
$minute = $HHMMSS_Time/100 % 100;
$second = $HHMMSS_Time % 100;

return New-Object System.DateTime $year, $month, $day, $hour, $minute, $second
}

# Create a new discovery

$oAPI = new-object -comObject "MOM.ScriptAPI"
$Discovery = $oAPI.CreateDiscoveryData(0, "$MPElement$", "$Target/Id$")

# Get known properties for the discovered instance
$oAPI.LogScriptEvent("ServiceReportingDiscovery.ps1", 100, 0, "Start Service Reporting service job discovery in $ConnectionString.")
try
{
$ErrorActionPreference = "Stop"

$connStr = "Data Source=" + $ConnectionString +";Database=msdb;Integrated Security=True;MultipleActiveResultSets=True;"
$sqlConnection = new-object System.Data.SqlClient.SqlConnection $connStr
$sqlConnection.Open()
$sqlCommand = $sqlConnection.CreateCommand()
$sqlCommand.CommandText = $CommandText

$sqlReader = $sqlCommand.ExecuteReader()

while ($sqlReader.Read())
{
$JobName = $sqlReader["Job Name"]
$ScheduleInterval = $sqlReader["Schedule Interval (Minutes)"]

# Create a discovered instance
$ServiceReportingServiceInstance = $Discovery.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ServiceReporting.2012.ServiceReportingService']$")

# Add properties to the discovered instance
$ServiceReportingServiceInstance.AddProperty("$MPElement[Name='SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService']/ConnectionString$", $ConnectionString)
$ServiceReportingServiceInstance.AddProperty("$MPElement[Name='SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService']/JobName$", $JobName)
$ServiceReportingServiceInstance.AddProperty("$MPElement[Name='SRLibraryMP!Microsoft.SystemCenter.ServiceReporting.ServiceReportingService']/ScheduleInterval$", $ScheduleInterval)

# Add the discovered instance
$Discovery.AddInstance($ServiceReportingServiceInstance)

$oAPI.LogScriptEvent("ServiceReportingDiscovery.ps1", 100, 0, "Found Service Reporting service job in $ConnectionString.")
}

# Return the discovery data.
$oAPI.LogScriptEvent("ServiceReportingDiscovery.ps1", 100, 0, "Finish Service Reporting service job discovery in $ConnectionString.")
$Discovery
}
catch
{
$oAPI.LogScriptEvent("ServiceReportingDiscovery.ps1", 100, 1, "Service Reporting service job discovery encountered exception: $_ .")
}
finally
{
$ErrorActionPreference = "Continue"
$sqlConnection.Close()
}


</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>ConnectionString</Name>
<Value>$Target/Property[Type="SQLLibrary!Microsoft.SQLServer.DBEngine"]/ConnectionString$</Value>
</Parameter>
<Parameter>
<Name>CommandText</Name>
<Value>DECLARE @JobName NVARCHAR(200) = 'Service Reporting DW System Job'
DECLARE @JobId UNIQUEIDENTIFIER, @ScheduleInterval INT

-- Job schedule
SELECT @JobId = j.job_id,
@ScheduleInterval = CASE
WHEN s.freq_type = 4 AND s.freq_interval = 1 AND s.freq_subday_type = 1 THEN 24 * 60 -- every 1 day, at specified time
WHEN s.freq_type = 4 AND s.freq_interval = 1 AND s.freq_subday_type = 2 THEN s.freq_subday_interval / 60 -- every 1 day, every n seconds
WHEN s.freq_type = 4 AND s.freq_interval = 1 AND s.freq_subday_type = 4 THEN s.freq_subday_interval -- every 1 day, every n minutes
WHEN s.freq_type = 4 AND s.freq_interval = 1 AND s.freq_subday_type = 8 THEN s.freq_subday_interval * 60 -- every 1 day, every n hours
ELSE 24 * 60 -- all others to be set as 1 day
END
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobschedules js
ON j.job_id = js.job_id
JOIN msdb.dbo.sysschedules s
ON s.schedule_id = js.schedule_id
WHERE j.name = @JobName

-- Output results
SELECT @JobName AS [Job Name],
@JobId AS [JobId],
ISNULL(@ScheduleInterval, 0) AS [Schedule Interval (Minutes)]
WHERE @JobId IS NOT NULL
</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</Discovery>