Windows Scheduled Task Discovery (Windows 2003)

Custom.Windows.ScheduledTask.DiscoverByScript (Discovery)

Discovers scheduled tasks on Windows Server 2003 computers

Knowledge Base article:

Summary

This discovery calls a script to retrieve a list of configured scheduled tasks from the computer. By default it runs every 15 minutes.

Element properties:

TargetCustom.Microsoft.Windows.ScheduledTask.Server.2003.OperatingSystem
EnabledTrue
Frequency555
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:
  • Custom.Windows.ScheduledTask
    • DisplayName
    • PrincipalName
    • TaskName
    • TaskPath
    • TaskDescription
    • TaskNextRunTime
    • TaskStatus
    • TaskLastRunTime
    • TaskLastResult
    • TaskSchedule
    • TaskCommand
    • TaskState
    • TaskUser
  • Custom.Windows.ScheduledTask.Repetitive
    • DisplayName
    • PrincipalName
    • TaskName
    • TaskPath
    • TaskDescription
    • TaskNextRunTime
    • TaskStatus
    • TaskLastRunTime
    • TaskLastResult
    • TaskSchedule
    • TaskCommand
    • TaskState
    • TaskUser
Discovered relationships and their attribuets:

Member Modules:

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

Source Code:

<Discovery ID="Custom.Windows.ScheduledTask.DiscoverByScript" Enabled="true" Target="Custom.Microsoft.Windows.ScheduledTask.Server.2003.OperatingSystem" ConfirmDelivery="false" Remotable="false" Priority="Normal">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Custom.Windows.ScheduledTask">
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
<Property TypeID="Windows!Microsoft.Windows.Computer" PropertyID="PrincipalName"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskName"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskPath"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskDescription"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskNextRunTime"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskStatus"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskLastRunTime"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskLastResult"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskSchedule"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskCommand"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskState"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskUser"/>
</DiscoveryClass>
<DiscoveryClass TypeID="Custom.Windows.ScheduledTask.Repetitive">
<Property TypeID="System!System.Entity" PropertyID="DisplayName"/>
<Property TypeID="Windows!Microsoft.Windows.Computer" PropertyID="PrincipalName"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskName"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskPath"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskDescription"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskNextRunTime"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskStatus"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskLastRunTime"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskLastResult"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskSchedule"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskCommand"/>
<Property TypeID="Custom.Windows.ScheduledTask" PropertyID="TaskState"/>
<Property TypeID="SchTaskLib!Custom.Windows.TaskSchedulerTask" PropertyID="TaskUser"/>
</DiscoveryClass>
<DiscoveryRelationship TypeID="SchTaskLib!Microsoft.Windows.Computer.Hosts.Custom.Windows.TaskSchedulerTask"/>
</DiscoveryTypes>
<DataSource ID="DScriptL" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<IntervalSeconds>555</IntervalSeconds>
<SyncTime/>
<ScriptName>Custom.Windows.ScheduledTask.ServiceDiscovery.vbs</ScriptName>
<Arguments>$MPElement$ $Target/Id$ $Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$ $Target/Property[Type="Custom.Microsoft.Windows.ScheduledTask.Server.2003.OperatingSystem"]/OSLanguage$</Arguments>
<ScriptBody><Script>'*************************************************************************
' Script Name - Custom.Windows.ScheduledTask.ServiceDiscovery.vbs
'
' Purpose - Queries for locally configured Scheduled Tasks and inserts them into OpsMgr
'
' Parameters 1 SourceID
' 2 ManagedEntity
' 3 TargetComputer
' 4 OSLanguage
'
' 04. June 2007 - Raphael Burri / [email protected]
' 10. December 2008 - Raphael Burri: Fix for scheduled tasks with more than a single schedule
' 22. January 2009 - Raphael Burri: No longer returning dates to ease load on the SCOM disocvery process
' 11. April 2009 - Raphael Burri: Including OSLanguage to support localized windows versions
' currently Dutch, English, French, German, Italian, Portuguese and Spanish are supported
' 12. June 2014 - Raphael Burri: changed DisplayString to match new Scheduler 2.0 MP (make views work properly)
'*************************************************************************

Option Explicit

'translations containing special characters (&#xE4; &#xF1; etc.) have to be built using the ASCII code

'Translation for DUTCH localization
Const NL_NOTASKS = "er zijn geen geplande taken op dit systeem" ' --&gt; checked on SP2 (Raphael)
Const NL_RUNNING = "Geactiveerd" ' --&gt; checked on SP2 (Raphael)
Const NL_ENABLED = "Ingeschakeld" ' --&gt; checked on SP2 (Raphael)
Const NL_DISABLED = "Uitgeschakeld" ' --&gt; checked on SP2 (Raphael)
'Const NL_NEVER = "Nooit" ' not used in this MP ' --&gt; checked on SP2 (Raphael)
Const NL_COULDNOTSTART = "Kan niet worden gestart" ' --&gt; checked on SP2 (Raphael)
Const NL_SCHEDDAILY = "Dagelijks"
Const NL_SCHEDWEEKLY = "Wekelijks"
Const NL_SCHEDMONTHLY = "Maandelijks"

'Translation for FRENCH localization
'Const F_NOTASKS = "aucune t&#xE2;che planifi&#xE9;e n'est pr&#xE9;sente" ' --&gt; checked on SP2 (Raphael)
Dim F_NOTASKS
F_NOTASKS = "aucune t" + Chr(131) + "che planifi" + Chr(130) + "e n'est pr" + Chr(130) + "sente"
Const F_RUNNING = "En cours" ' --&gt; info from Romain Girot
'Const F_ENABLED = "Activ&#xE9;e" ' --&gt; info from Romain Girot
Dim F_ENABLED
F_ENABLED = "Activ" + Chr(130) + "e"
'Const F_DISABLED = "D&#xE9;sactiv&#xE9;e" ' --&gt; info from Romain Girot
Dim F_DISABLED
F_DISABLED = "D" + Chr(130) + "sactiv" + Chr(130) + "e"
'Const F_NEVER = "Jamais" 'not used in this MP ' --&gt; info from Romain Girot
'Const F_COULDNOTSTART = "Non d&#xE9;marr&#xE9;e" ' --&gt; info from Romain Girot
Dim F_COULDNOTSTART
F_COULDNOTSTART = "Non d" + Chr(130) + "marr" + Chr(130) + "e"
Const F_SCHEDDAILY = "Tous les jours"
Const F_SCHEDWEEKLY = "Toutes les semaines"
Const F_SCHEDMONTHLY = "Tous les mois"

'Translation for GERMAN localization
Const D_NOTASKS = "keine geplanten Tasks auf dem System vorhanden" ' --&gt; checked on SP2 (Raphael)
'Const D_RUNNING = "Wird ausgef&#xFC;hrt" ' --&gt; checked on SP2 (Raphael)
Dim D_RUNNING
D_RUNNING = "Wird ausgef" + Chr(129) + "hrt"
Const D_ENABLED = "Aktiviert" ' --&gt; checked on SP2 (Raphael)
Const D_DISABLED = "Deaktiviert" ' --&gt; checked on SP2 (Raphael)
'Const D_NEVER = "Nie" ' not used in this MP ' --&gt; checked on SP2 (Raphael)
Const D_COULDNOTSTART = "Nicht gestartet" ' --&gt; checked on SP2 (Raphael)
Dim D_SCHEDDAILY
D_SCHEDDAILY = "T" + Chr(132) + "glich"
Dim D_SCHEDWEEKLY
D_SCHEDWEEKLY = "W" + Chr(148) + "chentlich"
Const D_SCHEDMONTHLY = "Monatlich"

'Translation for ITALIAN localization
Const I_NOTASKS = "nessuna operazione pianificata presente" ' --&gt; checked on SP2 (Raphael)
Const I_RUNNING = "In esecuzione" ' --&gt; checked on SP2 (Raphael)
Const I_ENABLED = "Abilitata" ' --&gt; checked on SP2 (Raphael)
Const I_DISABLED = "Disabilitata" ' --&gt; checked on SP2 (Raphael)
'Const I_NEVER = "Mai avviata" ' not used in this MP ' --&gt; checked on SP2 (Raphael)
Const I_COULDNOTSTART = "Impossibile avviare"
Const I_SCHEDDAILY = "Ogni giorno"
Const I_SCHEDWEEKLY = "Ogni settimana"
Const I_SCHEDMONTHLY = "Ogni mese"

'Translation for PORTUGUESE localization
'Const P_NOTASKS = "N&#xE3;o existem tarefas agendadas" ' --&gt; checked on SP2 (Raphael)
Dim P_NOTASKS
P_NOTASKS = "N" + Chr(198) + "o existem tarefas agendadas"
Const P_RUNNING = "A executar" ' --&gt; checked on SP2 (Raphael)
Const P_ENABLED = "Activado" '(sic!) female as well - should end with a ' --&gt; checked on SP2 (Raphael)
Const P_DISABLED = "Desactivado" '(sic!) female as well - should end with a ' --&gt; checked on SP2 (Raphael)
'Const P_NEVER = "Nunca" ' not used in this MP ' --&gt; checked on SP2 (Raphael)
'Const P_COULDNOTSTART = "N&#xE3;o foi poss&#xED;vel iniciar" ' --&gt; checked on SP2 (Raphael)
Dim P_COULDNOTSTART
P_COULDNOTSTART = "N" + Chr(198) + "o foi poss" + Chr(161) + "vel iniciar"
Const P_SCHEDDAILY = "Diariamente"
Const P_SCHEDWEEKLY = "Semanalmente"
Const P_SCHEDMONTHLY = "Mensalmente"

'Translation for SPANISH localization
Const S_NOTASKS = "no hay tareas programadas" ' --&gt; checked on SP2 (Raphael)
'Const S_RUNNING = "Ejecut&#xE1;ndose" ' --&gt; checked on SP2 (Raphael)
Dim S_RUNNING
S_RUNNING = "Ejecut" + Chr(160) + "ndose"
Const S_ENABLED = "Habilitada" ' --&gt; checked on SP2 (Raphael)
Const S_DISABLED = "Deshabilitado" '(sic!) tarea is female so this should really be Deshabilitada ' --&gt; checked on SP2 (Raphael)
'Const S_NEVER = "Nunca" ' not used in this MP ' --&gt; checked on SP2 (Raphael)
Const S_COULDNOTSTART = "No se pudo iniciar" ' --&gt; checked on SP2 (Raphael)
Const S_SCHEDDAILY = "Diariamente"
Const S_SCHEDWEEKLY = "Semanalmente"
Const S_SCHEDMONTHLY = "Mensualmente"

'Command to execute (get a CSV list of the scheduled taks on the local machine)
Const STRCOMMAND = "schtasks.exe /query /FO CSV /NH /V"
Const CSVSEPARATOR = ""","""

Dim objArgs, objAPI, objDiscoveryData
Dim SourceID, ManagedEntityId, TargetComputer, strOutput, strOSLang
Dim iLine, iLines, iTask, iSched
Dim aOutput, aSchedTask, aTempDate, aDiscoverDataTask(13), aDiscoveryData
Dim bTasksPresent

'get the parameters from the management pack
Set objArgs = WScript.Arguments
If objArgs.Count &lt; 4 Then
WScript.Quit -1
End If
SourceID = objArgs(0)
ManagedEntityId = objArgs(1)
TargetComputer = objArgs(2)
strOSLang = objArgs(3)


Set objAPI = CreateObject("MOM.ScriptAPI")

'fetch the command output
aOutput = RunCommand(STRCOMMAND)

Dim l
For l = 0 To UBound(aOutput) - 1
'WScript.Echo aOutput(l)
'WScript.Echo VbCrLf + VbCrLf

Next


'see if schedued tasks are present on this computer
Select Case strOSLang
Case "ENGLISH"
If InStr(aOutput(0), "no scheduled tasks present") = 0 Then
bTasksPresent = vbTrue
End If
Case "DUTCH"
If InStr(aOutput(0), NL_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If
Case "FRENCH"
If InStr(aOutput(0), F_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If
Case "GERMAN"
If InStr(aOutput(0), D_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If
Case "ITALIAN"
If InStr(aOutput(0), I_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If
Case "PORTUGUESE"
If InStr(aOutput(0), P_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If
Case "SPANISH"
If InStr(aOutput(0), S_NOTASKS) = 0 Then
bTasksPresent = vbTrue
End If

End Select


'only procss data if scheduled tasks were found
If bTasksPresent = vbTrue Then

Set objDiscoveryData = objAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

'prepare an array to fill the scheduled tasks' data in
iLines = UBound(aOutput) + 1
ReDim aDiscoveryData(iLines)

For iLine = 0 To iLines - 1
'fill an array with the values used to later populate discovery
aSchedTask = Split(aOutput(iLine), CSVSEPARATOR)

'Task Number
aDiscoverDataTask(0) = iLine + 1

'evaluate if a task has a repetitive schedule (based on Schedule Type - column 14)
aDiscoverDataTask(12) = vbFalse
'French 'Tous les jours' contains a white space at the end - so trim it first
aSchedTask(13) = Trim(aSchedTask(13))
Select Case strOSLang
Case "ENGLISH"
If aSchedTask(13) = "Daily" Or aSchedTask(13) = "Weekly" Or aSchedTask(13) = "Monthly" Then
aDiscoverDataTask(12) = vbTrue
End If
Case "DUTCH"
If aSchedTask(13) = NL_SCHEDDAILY Or aSchedTask(13) = NL_SCHEDWEEKLY Or aSchedTask(13) = NL_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
End If
Case "FRENCH"
'WScript.Echo "Task " &amp; aDiscoverDataTask(0) &amp; ": " &amp; aSchedTask(1) &amp; ": '" &amp; aSchedTask(13) &amp; "'"
If aSchedTask(13) = F_SCHEDDAILY Or aSchedTask(13) = F_SCHEDWEEKLY Or aSchedTask(13) = F_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
'WScript.Echo "Is repetitive"
End If
Case "GERMAN"
If aSchedTask(13) = D_SCHEDDAILY Or aSchedTask(13) = D_SCHEDWEEKLY Or aSchedTask(13) = D_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
End If
Case "ITALIAN"
If aSchedTask(13) = I_SCHEDDAILY Or aSchedTask(13) = I_SCHEDWEEKLY Or aSchedTask(13) = I_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
End If
Case "PORTUGUESE"
If aSchedTask(13) = P_SCHEDDAILY Or aSchedTask(13) = P_SCHEDWEEKLY Or aSchedTask(13) = P_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
End If
Case "SPANISH"
If aSchedTask(13) = S_SCHEDDAILY Or aSchedTask(13) = S_SCHEDWEEKLY Or aSchedTask(13) = S_SCHEDMONTHLY Then
aDiscoverDataTask(12) = vbTrue
End If
End Select


'Task Name
aDiscoverDataTask(1) = aSchedTask(1)

'if the task has more than one schedule, then we need to take that into account
'but only if it isn't the first task being processed
aDiscoverDataTask(11) = ""
If iLine &gt; 0 And iLine &lt; iLines Then
'check if the task name already exists and add a skip flag if so
For iTask = 0 To iLine - 1
'WScript.Echo iTask &amp; ": " &amp; iLine &amp; ": " &amp; aDiscoveryData(iTask)(1)
If aDiscoverDataTask(1) = aDiscoveryData(iTask)(1) Then
'task with that name has already been added - so skip this one
aDiscoverDataTask(11) = "skip: multiple schedules"
'WScript.Echo "Task " &amp; aDiscoverDataTask(0) &amp; " was marked as multiple schedule and is going to be skipped"
'write that information to the schedule field of the preceeding task if it hasn't been marked already
If aDiscoveryData(iTask)(11) = "" Then
aDiscoveryData(iTask)(6) = "Multiple schedule times"
aDiscoveryData(iTask)(11) = "use: multiple schedules"
'WScript.Echo "Task " &amp; aDiscoveryData(iTask)(0) &amp; " was marked as multiple schedule and is going to be used"
End If
'if any of the schedules is repetitive, the whole task is classified as repetitive
'so set the flag for the other tasks accordingly (only the first one is actually going to be disocvered by SCOM)
If aDiscoverDataTask(12) = vbTrue Then
'set the preceeding task number index
iSched = iTask
'if the two task names match (current and preceeding), set the repetitive flag on the preceeding
'WScript.Echo "iSched: " &amp; iSched &amp; " / aDiscoverDataTask(1): " &amp; aDiscoverDataTask(1) &amp; " / iTask: " &amp; iTask
While aDiscoveryData(iSched)(1) = aDiscoverDataTask(1) And Not iSched = 0
aDiscoveryData(iSChed)(12) = vbTrue
'WScript.Echo aDiscoverDataTask(0) &amp; " sets task " &amp; aDiscoveryData(iSched)(0) &amp; " as repetitive"
'WScript.Echo aDiscoveryData(iSChed)(1) &amp; " INdex N&#xB0;: " &amp; iSched
'decrement the task number to get the preceeding tasks' index
iSched = iSched - 1

Wend

End If
End If
Next
End If

'Task Next Run Time - put date before time to make the string a bit easier to read
If IsDate(aSchedTask(2)) Then
aDiscoverDataTask(2) = aSchedTask(2)
Else
On Error Resume Next
aTempDate = Split(aSchedTask(2), ",")
aDiscoverDataTask(2) = CDate(Trim(aTempDate(1)) &amp; " " &amp; Trim(aTempDate(0)))
On Error Goto 0
End If
'if it is a date, return just a string indicating that this is the case
'that way SCOM has less discovery changes to write back
If IsDate(aDiscoverDataTask(2)) Then
aDiscoverDataTask(2) = "HasDate"
Else
aDiscoverDataTask(2) = "NotDefined"
End If


'Task Status (4th column)
On Error Resume Next
aDiscoverDataTask(3) = CStr(aSchedTask(3))
If Err.number &lt;&gt; 0 Then
aDiscoverDataTask(3) = "Could not start"
End If
On Error Goto 0

Select Case strOSLang
Case "ENGLISH"
'it is not particularly useful to know if the task is currently running or not - so leave it out (less change to the DB)
If aDiscoverDataTask(3) = "Running" Then
aDiscoverDataTask(3) = ""
End If
Case "DUTCH"
If aDiscoverDataTask(3) = NL_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = NL_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If
Case "FRENCH"
If aDiscoverDataTask(3) = F_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = F_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If
Case "GERMAN"
If aDiscoverDataTask(3) = D_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = D_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If
Case "ITALIAN"
If aDiscoverDataTask(3) = I_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = I_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If
Case "PORTUGUESE"
If aDiscoverDataTask(3) = P_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = P_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If
Case "SPANISH"
If aDiscoverDataTask(3) = S_RUNNING Then
aDiscoverDataTask(3) = ""
ElseIf aDiscoverDataTask(3) = S_COULDNOTSTART Then
aDiscoverDataTask(3) = "Could not start"
End If

End Select

'Task Last Run Time (6th column)
If IsDate(aSchedTask(5)) Then
aDiscoverDataTask(4) = aSchedTask(5)
Else
On Error Resume Next
aTempDate = Split(aSchedTask(5), ",")
aDiscoverDataTask(4) = CDate(Trim(aTempDate(1)) &amp; " " &amp; Trim(aTempDate(0)))
On Error Goto 0
End If
'if it is a date, return just a string indicating that this is the case
'that way SCOM has less discovery changes to write back
If IsDate(aDiscoverDataTask(4)) Then
aDiscoverDataTask(4) = "HasDate"
Else
aDiscoverDataTask(4) = "Never"
End If


'Task Last Result (7th column)
aDiscoverDataTask(5) = aSchedTask(6)
'Task Schedule (9th column)
aDiscoverDataTask(6) = aSchedTask(8)

'Task Command (10th column)
aDiscoverDataTask(7) = aSchedTask(9)

'Task State (13th column)
Select Case strOSLang
Case "ENGLISH"
aDiscoverDataTask(8) = aSchedTask(12)
Case "DUTCH"
If aSchedTask(12) = NL_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = NL_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If
Case "FRENCH"
If aSchedTask(12) = F_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = F_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If
Case "GERMAN"
If aSchedTask(12) = D_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = D_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If
Case "ITALIAN"
If aSchedTask(12) = I_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = I_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If
Case "PORTUGUESE"
If aSchedTask(12) = P_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = P_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If
Case "SPANISH"
If aSchedTask(12) = S_ENABLED Then
aDiscoverDataTask(8) = "Enabled"
ElseIf aSchedTask(12) = S_DISABLED Then
aDiscoverDataTask(8) = "Disabled"
End If

End Select

'Task User
aDiscoverDataTask(9) = aSchedTask(19)
'Task Comments
aDiscoverDataTask(10) = aSchedTask(11)



'WScript.Echo "Scheduled Task N&#xB0; " &amp; iLine + 1 &amp; ": " &amp; aDiscoverDataTask(1) &amp; VbCrLf &amp; _
' " - Schedule: " &amp;aDiscoverDataTask(6) &amp; VbCrLf &amp; _
' " - Skip: " &amp; aDiscoverDataTask(11) &amp; VbCrLf &amp; VbCrLf



'create a new entry in the main array
aDiscoveryData(iLine) = aDiscoverDataTask
Next

'Create Discovery Information in OpsMgr
Set objDiscoveryData = DiscoverSchedTask(objDiscoveryData, aDiscoveryData)
Else
'Call objAPI.LogScriptEvent("Custom.Windows.ScheduledTask.ServiceDiscovery.vbs", 8099, 4, "The service discovery script has not found any Scheduled Tasks and is closing without writing discovery data.")
'return an empty discovery data bag
Set objDiscoveryData = objAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

End If


Call objAPI.Return(objDiscoveryData)


Set objDiscoveryData = Nothing
Set objAPI = Nothing

WScript.Quit(0)

Function RunCommand(ByVal strCommand)

Dim objShell, objExecObject, objDictionary
Dim strOutput, strLine, strPrevLine
Dim iLine, iFragmentLine

Set objShell = WScript.CreateObject("WScript.Shell")
Set objDictionary = WScript.CreateObject("Scripting.Dictionary")
On Error Resume Next
Set objExecObject = objShell.Exec(strCommand)
If Not Err.number = 0 Then
'WScript.Echo "Command " + STRCOMMAND + " failed to execute."
Else

Do While Not objExecObject.StdOut.AtEndOfStream
iLine = iLine + 1
'the description could contain line feeds. need to check that!
'localized characters could also lead to line feeds
'the line needs 27 column fields. If it isn't the case it has been divided by the comment field or anything else.
'Needs being put together as a single dictionary object.
strLine = objExecObject.StdOut.ReadLine()
'WScript.Echo UBound(Split(strLine, CSVSEPARATOR))
If UBound(Split(strLine, CSVSEPARATOR)) &lt; 27 Then
'this is a fragment line - needs being pasted together
'Check if it is a first line
If iFragmentLine = 0 Then
'add the line to the dictionary and increment the fragment counter
objDictionary.Add iLine, strLine
'WScript.Echo "This is a 1st Fragment line"
iFragmentLine = iFragmentLine + 1
Else
'add additional line to previous dictionary entry
'WScript.Echo "This is a second fragment line"
iLine = iLine - 1
strPrevLine = objDictionary.Item(iLine)
objDictionary.Remove iLine
objDictionary.Add iLine, strPrevLine &amp; strLine
'check if the line is now complete (27 columns)
'WScript.Echo "Second fragment column count: " &amp; UBound(Split(objDictionary.Item(iLine), CSVSEPARATOR))
If UBound(Split(objDictionary.Item(iLine), CSVSEPARATOR)) &gt;= 27 Then
'reset the fragment counter
iFragmentLine = 0
Else
'increment the fragment counter
iFragmentLine = iFragmentLine + 1
End If
End If
Else
'normal entry
objDictionary.Add iLine, strLine
iFragmentLine = 0
End If
'WScript.Echo iLine &amp; ": " &amp; strLine &amp; VbCrLf
Loop
End If
On Error Goto 0
RunCommand = objDictionary.Items
End Function

Function DiscoverSchedTask (ByVal objDiscoveryData, ByVal aDiscoverData)

Dim objInst
Dim iTasks
Dim aScheduledTask

If UBound(aDiscoverData) &gt; 0 Then
For iTasks = 0 To UBound(aDiscoverData) -1
aScheduledTask = aDiscoverData(iTasks)
'if the task has multiple schedules, it must not be processed again
If Not InStr(aScheduledTask(11), "skip") &gt; 0 Then
Call objAPI.LogScriptEvent("Custom.Windows.ScheduledTask.ServiceDiscovery.vbs", 8001, 4, "The service discovery script is writing discovery data for Scheduled Task Number " + CStr(aScheduledTask(0)) + "." + VbCrLf _
+ "Name: " + aScheduledTask(1) + VbCrLf _
+ "Command: " + VbCrLf _
+ aScheduledTask(7))

'tasks with a repetitive schedule have a more specialized class
If aScheduledTask(12) = vbTrue Then
Set objInst = objDiscoveryData.CreateClassInstance("$MPElement[Name='Custom.Windows.ScheduledTask.Repetitive']$")
Else
Set objInst = objDiscoveryData.CreateClassInstance("$MPElement[Name='Custom.Windows.ScheduledTask']$")
End If
Call objInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputer)
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskPath$", aScheduledTask(1))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskName$", aScheduledTask(1))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskDescription$", CStr(aScheduledTask(10)))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskNextRunTime$", CStr(aScheduledTask(2)))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskLastRunTime$", CStr(aScheduledTask(4)))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskStatus$", CStr(aScheduledTask(3)))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskLastResult$", aScheduledTask(5))
Call objInst.AddProperty("$MPElement[Name='Custom.Windows.ScheduledTask']/TaskSchedule$", aScheduledTask(6))
Call objInst.AddProperty("$MPElement[Name='Custom.Windows.ScheduledTask']/TaskCommand$", aScheduledTask(7))
Call objInst.AddProperty("$MPElement[Name='Custom.Windows.ScheduledTask']/TaskState$", aScheduledTask(8))
Call objInst.AddProperty("$MPElement[Name='SchTaskLib!Custom.Windows.TaskSchedulerTask']/TaskUser$", aScheduledTask(9))
Call objInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "Task: " &amp; aScheduledTask(1))

Call objDiscoveryData.AddInstance(objInst)

Else
'WScript.Echo "Skipping task N&#xB0; " &amp; iTasks &amp; ": " &amp; aScheduledTask(1)
End If


Next
Set DiscoverSchedTask = objDiscoveryData

Set objInst = Nothing


End If

End Function</Script></ScriptBody>
<TimeoutSeconds>120</TimeoutSeconds>
<EventPolicy>
<StdOutMatches/>
<StdErrMatches>\a+</StdErrMatches>
<!--
<ExitCodeMatches>[^0]+</ExitCodeMatches>
-->
<ExitCodeMatches>[^(0|128)]+</ExitCodeMatches>
</EventPolicy>
</DataSource>
</Discovery>