Announcement

Collapse
No announcement yet.

Scheduled task failure notification

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scheduled task failure notification

    I have used blat and Mapisend to "filewatch" based on a scheduled task to mail contents of a share as an attachment.

    what I would like to do is create some sort of task scheduler monitor to mail VIA blat/mapisend (or other method) failures of a scheduled task.

    How to I script such a thing?

  • #2
    Re: Scheduled task failure notification

    What type of failure with the task? Meaning, it just fails to run?

    Comment


    • #3
      Re: Scheduled task failure notification

      An example from: http://www.activexperts.com/activmon...askscheduling/

      NOTE*** This method cannot enumerate tasks created using the Task Scheduler, it can only enumerate tasks created with the AT.exe utility. I'm sure a batch scripting wizard would be able to use "SCHTASKS.exe /query" to find the job status...

      Code:
      strComputer = "."
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set colScheduledJobs = objWMIService.ExecQuery _
          ("Select * from Win32_ScheduledJob")
      For Each objJob in colScheduledJobs
          Wscript.Echo "Caption: " & objJob.Caption
          Wscript.Echo "Command: " & objJob.Command
          Wscript.Echo "Days Of Month: " & objJob.DaysOfMonth
          Wscript.Echo "Days Of Week: " & objJob.DaysOfWeek
          Wscript.Echo "Description: " & objJob.Description
          Wscript.Echo "Elapsed Time: " & objJob.ElapsedTime
          Wscript.Echo "Install Date: " & objJob.InstallDate
          Wscript.Echo "Interact with Desktop: " & objJob.InteractWithDesktop
          Wscript.Echo "Job ID: " & objJob.JobID
          Wscript.Echo "Job Status: " & objJob.JobStatus
          Wscript.Echo "Name: " & objJob.Name
          Wscript.Echo "Notify: " & objJob.Notify
          Wscript.Echo "Owner: " & objJob.Owner
          Wscript.Echo "Priority: " & objJob.Priority
          Wscript.Echo "Run Repeatedly: " & objJob.RunRepeatedly
          Wscript.Echo "Start Time: " & objJob.StartTime
          Wscript.Echo "Status: " & objJob.Status
          Wscript.Echo "Time Submitted: " & objJob.TimeSubmitted
          Wscript.Echo "Until Time: " & objJob.UntilTime
      Next
      You will most likely want the job status. You can configure this to run after your task if you want, and have it send you an email with the results.
      Last edited by ekrengel; 22nd October 2009, 17:22.

      Comment


      • #4
        Re: Scheduled task failure notification

        It is also possible to read the scheduler log file (c:\windows\SchedLgU.txt).

        The sample below read the log and collect data per event of all events.

        next,
        The output filter is starting at line 81, at each case (1-6) you can set criteria to exclude events from strOUT. In the script below only events from within the last 24 hours where Job-Result is not SUCCESS will be showed in the popup.

        Code:
        ' ## Parse the SchedLgU.txt file. ##
        '
        ' Author: Remco Simons [NL] 2009
        ' http://forums.petri.com/showthread.php?t=41174
        '
        '  Note:
        '   This script is programmed (at line 37 in this script)
        '   to stop further reading the log file if
        '   it get to this line in the SchedLgU logfile:
        '     [ ***** Most recent entry is above this line ***** ]
        '   You can delete this limitation from script if you like.) 
        
        
        '*** Open the Log file for reading
        On Error Resume Next
        Dim WinDir, FileName, newJob, ts
        With CreateObject("Shell.Application").Namespace(&H24).Self
           WinDir = .Path
        End With
        FileName = WinDir & "\SchedLgU.txt"
        With CreateObject("Scripting.FileSystemObject")
           Set ts = .GetFile(FileName).OpenAsTextStream(1, -1)
           If err.Number <> 0 Then _
             wscript.echo "Logfile not available": wscript.quit
        End With
        On Error Goto 0
        
        '*** Global Declarations
        DIM currJob, job(),y,z
        Dim strDate, strJobName, strCmdLn
        Dim strEventType, strErr, strComments
        
        '*** Start Reading the log lines 
        Do While ts.AtEndOfStream <> True
           TextLine = Trim(ts.ReadLine)
        
           If Left(TextLine, 8) = "[ ***** " Then exit Do
        
          '*** Find starting line of each job event
           If Instr(TextLine, ".job""") > 0 AND _
             Left(TextLine, 1) <> vbTab Then
        
             Add_To_2D_Array strDate, strJobName, _
                 strCmdLn, strEventType, strErr, strComments
        
             newJob = True: ParseTheFirstline TextLine
             currJob = strJobName
        
          '*** Read other event lines of the Job
           ElseIf newJob = True AND Left(TextLine, 1) = vbTab Then
             TextLine = Trim(Replace(TextLine, vbTab, ""))
        
             If Len(strDate) = 0 Then
               RetrieveDate TextLine 
             Else
               ParseTheOtherlines TextLine
             End If
        
          '*** no job event, continue with searching
           Else
               newJob = False
           End If
        Loop
        
        ts.Close
        
        Add_To_2D_Array strDate, strJobName, _
             strCmdLn, strEventType, strErr, strComments
        
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim Item, iCnt, pre, strName, strOut
        For z = 0 to UBound(job,2)
           Item = Empty : iCnt = 0
        
           For y = 0 to UBound(job,1)
             If Not IsEmpty(Item) Then pre = Item & vbNewLine 
             iCnt = iCnt + 1    
        
             Select Case iCnt
        
               Case 1
                 strName = "Date"
                 strVal = job(y,z)
                 If DateDiff("h", strVal, Now) >24 Then
                   Item = Empty : Pre = Empty
                   exit For
                 End If
        
               Case 2
                 strName = "LogName"
        
               Case 3
                 strName = "Command-Line"
        
               Case 4
                 strName = "Event-type"
        
               Case 5
                 strName = "Result"
                 strVal = job(y,z)
                 If UCase(strVal) = UCase("Success") Then
                   Item = Empty : Pre = Empty
                   exit For
                 End If
         
               Case 6
                 strName = "Comments"
        
             End Select
             Item = pre & strName & ": " & job(y,z)
             Pre = Empty
           Next
        
           If Len(Item) <> 0 Then
             If Not IsEmpty(strOut) Then pre = strOut & vbNewLine & vbNewLine
             strOut = pre & Item : Pre = Empty
           End If
        Next
        
        If Len(Trim(strOut)) >0 Then
           ' Send MAIL
            wscript.echo strOut
        End If
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        
        Sub ParseTheFirstline(theString)
           Dim DoneTextBetweenParentheses
           Dim oRE, oChar, iCnt
           Dim Opening, Closing
           
           strJobName = Empty
           strCmdLn = Empty
           strErr = Empty
           strDate = Empty
           strEventType = Empty
           strComments = Empty
        
           strJobName = Mid(theString,2,Instr(Mid(theString,2), ".job""")-1)
        
           LenString = Len(theString)
           If InstrRev(theString, "**") = LenString -1 Then
             strEventType = "Run"
             strErr = "Failed"
             strbegin = InstrRev(theString, ")") +2
             strlength = InstrRev(theString, "**", LenString -2) -(strbegin +1)
             On Error Resume Next
             strDate = CDate( Mid(theString, strbegin, strlength) )
             If err.Number <> 0 Then
               err.Clear: strDate = Empty
             End If
             On Error Goto 0
             strComments = Mid(theString, Instr(theString, "**"))
           End If
        
           Set oRE = New Regexp : oRE.Global = True
           oRE.Pattern = "."
        
           Opening = 0 : Closing = 0 : iCnt = 0
           For Each oChar In oRE.Execute(theString)
             iCnt = iCnt +1
        
            ' If NOT DoneTextBetweenParentheses = True Then
               If oChar.Value = "(" Then Opening = Opening +1
               If oChar.Value = ")" Then Closing = Closing +1
               If iCnt >1 AND Closing >0 Then
                 DoneTextBetweenParentheses = True
                 Opening = Instr(theString, "(") +1 : Closing = iCnt
                 strCmdLn = Mid(theString, Opening, Closing - Opening)
                 exit For '*
               End If
            ' End If
           Next
        
        End Sub
        
        Sub RetrieveDate(theString)
           Dim Pos1, Pos2, sLength
           Dim oRE, oChar, RevLine
           Pos1 = Empty
        
           Set oRE = New Regexp : oRE.Global = True
           oRE.Pattern = "."
        
           For Each oChar In oRE.Execute(theString)
             RevLine = oChar & RevLine
             If Pos1 = Empty Then
               If IsNumeric(oChar.Value) Then _
                 Pos1 = Instr(theString, oChar.Value)
             End If
           Next
           For Each oChar In oRE.Execute(RevLine)
             If IsNumeric(oChar.Value) Then
               Pos2 = InstrRev(theString, oChar.Value)
               exit For
             End If
           Next
        
           strEventType = Trim(Left(theString, Pos1 -1))
           If Len(strErr) = 0 Then strErr = "Success"
        
           On Error Resume Next
           sLength = (Pos2 +1) -Pos1
           strDate = CDATE(Mid(theString, Pos1, sLength))
           If err.Number <> 0 Then
             strDate = CDate( #1/1/1601# )
             err.Clear
           End If             
           On Error Goto 0
        
        End Sub
        
        Sub ParseTheOtherlines(theString)
           Dim exitCode, sBegin, sLength
        
           If lcase(strErr) = lcase("Success") Then
             exitCode = Empty : sBegin = InstrRev(theString, "(" )+1
             If sBegin > 1 Then
               sLength = InstrRev(theString, ")") - sBegin
               If sLength > 0 Then _
                 exitCode = Mid(theString, sBegin, sLength)
             End If
             If exitCode <> 0 Then strErr = "Failed"
           End If
           If Len(strComments) > 0 Then
             strComments = strComments & " " & theString
           Else
             strComments = theString
           End If
        End Sub
        
        Sub Add_To_2D_Array(Dt, JobName, CommandL, EventType, Reslt, Comments)
           If Len(currJob) <> 0 Then
             y = 5 : z = 0 
             On Error Resume Next
             z = UBound(job,2) + 1
             On Error Goto 0
        
             redim preserve job(y,z)
             job(0,z) = Dt
             job(1,z) = JobName
             job(2,z) = CommandL
             job(3,z) = EventType
             job(4,z) = Reslt
             job(5,z) = Comments
             currJob = Empty
           End If
        End Sub
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Scheduled task failure notification

          Very nice!

          Comment


          • #6
            Re: Scheduled task failure notification

            Actually, that's a lot of complicated code that I'm just like "wow I wish I could do that"

            Can you add some comments in Rems (really just in the second half of the script with all the subs)? I'd want to try and understand a lot better so I can implement in other scripts where this might be useful. And to just increase my skills
            Last edited by ekrengel; 27th October 2009, 14:52.

            Comment

            Working...
            X