none
Schedule Task success/disable/failure

    Question

  • Hi ,

    I came across a requirement where we need to monitor the schedule job status(Success and Failure), With event log i don't find it worth.

    Alternate to Windows event log , i tried this:

    cmd# schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo LIST /v

    HostName: JOSMAHEN2

    TaskName: \Microsoft\Windows\TestTask\Backup
    Next Run Time: 22/02/2012 10:10:35 PM
    Status: Ready
    Logon Mode: Interactive/Background
    Last Run Time: 22/02/2012 10:07:51 PM
    Last Result: 0
    Author: ASIAPACIFIC\josmahen
    Task To Run: C:\Users\josmahen\Desktop\backup.bat
    Start In: N/A
    Comment: N/A
    Scheduled Task State: Enabled
    Idle Time: Disabled
    Power Management: Stop On Battery Mode, No Start On Batteries
    Run As User: JOSMAHEN2\mjoshi
    Delete Task If Not Rescheduled: Enabled
    Stop Task If Runs X Hours and X Mins: 72:00:00
    Schedule: Scheduling data is not available in this format.
    Schedule Type: Daily
    Start Time: 2:00:35 PM
    Start Date: 21/02/2012
    End Date: N/A
    Days: Every 1 day(s)
    Months: N/A
    Repeat: Every: 0 Hour(s), 5 Minute(s)
    Repeat: Until: Time: None
    Repeat: Until: Duration: Disabled
    Repeat: Stop If Still Running: Disabled

    If the Last Result:0 , which suggest its an success job occurred.

    Now my requirement is to build a solution which will be simple to monitor my Schedule job Success/failed/disabled status. How can i achieve the same?

    Regards,

    Mahender Joshi

    Tuesday, February 28, 2012 7:44 AM

Answers

  • It may be easier as a CSV:

    schtasks /query /fo csv > schtasks.csv
    $csv=cat schtasks.csv
    $csv[1..999]|out-file schtasks.csv
    import-csv schtasks.csv


    ¯\_(ツ)_/¯

    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:22 AM
    • Unmarked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    Tuesday, February 28, 2012 9:18 AM
  • It may be easier as a CSV:

    schtasks /query /fo csv > schtasks.csv
    $csv=cat schtasks.csv
    $csv[1..999]|out-file schtasks.csv
    import-csv schtasks.csv


    ¯\_(ツ)_/¯

    What's the purpose of the middle two lines?  This seems to work just fine:

    schtasks /query /fo csv > schtasks.csv
    import-csv schtasks.csv


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    Tuesday, February 28, 2012 9:24 AM

All replies

  • It seems you already posted the solution: Issue the command "schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo LIST /v", then check the response in the "Last Result" field.
    Tuesday, February 28, 2012 8:39 AM
  • Yes correct, but i want to make it more easier , with some scripting like :

    The script will get the JobName from a Imported Text file(Consider for a single Job Name)

    The Output result will be saved into Output.txt file having only :  "Last Result" value and "Scheduled Task State" value .

    Pegasus, Do you have any better way to apart from this, please help.

    Regards,

    Mahender Joshi


    • Edited by mj010207 Tuesday, February 28, 2012 8:50 AM
    Tuesday, February 28, 2012 8:49 AM
  • This is how I'd do it in Powershell (inevitably!).  The $result is an integer that can be tested, and an appropriate email can be sent with Send-Mailmessage:

    ((schtasks /query /TN '\Microsoft\Windows\TestTask\Backup' /fo LIST /v) | Out-String) -match 'Last Result:(.+)'
    $result = [int]$matches[1].trim()
    $result


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Tuesday, February 28, 2012 8:51 AM
    Tuesday, February 28, 2012 8:49 AM
  • Teddy,

    I tried to run the powershell but its failing:

    C:\Users\josmahen\Desktop\Test Script>cscript Run.vbs
    Microsoft (R) Windows Script Host Version 5.8
    Copyright (C) Microsoft Corporation. All rights reserved.


    C:\Users\josmahen\Desktop\Test Script\Run.vbs(1, 1) Microsoft VBScript compilation error: Expected statement

    powershell:

    ((schtasks /query /TN '\Microsoft\Windows\TestTask\Backup' /fo LIST /v) | Out-String) -match 'Last Result:(.+)'
    $result = [int]$matches[1].trim()
    $result

    Tuesday, February 28, 2012 9:10 AM
  • Sorry to confuse the issue.  The script I posted is Powershell, not VBS.  You are trying to run it as a .vbs script, and this won't work.  If you need this done in VBS, someone else will have to help:  It's just not a strong point of mine.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, February 28, 2012 9:15 AM
  • It may be easier as a CSV:

    schtasks /query /fo csv > schtasks.csv
    $csv=cat schtasks.csv
    $csv[1..999]|out-file schtasks.csv
    import-csv schtasks.csv


    ¯\_(ツ)_/¯

    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:22 AM
    • Unmarked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    Tuesday, February 28, 2012 9:18 AM
  • It may be easier as a CSV:

    schtasks /query /fo csv > schtasks.csv
    $csv=cat schtasks.csv
    $csv[1..999]|out-file schtasks.csv
    import-csv schtasks.csv


    ¯\_(ツ)_/¯

    What's the purpose of the middle two lines?  This seems to work just fine:

    schtasks /query /fo csv > schtasks.csv
    import-csv schtasks.csv


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by mj010207 Wednesday, February 29, 2012 10:23 AM
    Tuesday, February 28, 2012 9:24 AM
  • On XP and WS2003 the output starts with a blank line.


    ¯\_(ツ)_/¯

    Tuesday, February 28, 2012 11:07 AM
  • Hi,

    I want to more fine tune into this:

    Is it possible to get the output result in text file as :

    The Next Run-time is $Time

    The Job Status is  $ Ready or Could not start

    Wednesday, February 29, 2012 9:59 AM
  • Have you tried any of the script suggestions so far?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 29, 2012 10:03 AM
  • Teddy, i have used script from JRV, from that i am having output result in csv as 


    TaskName Next Run Time Status
    \At1 1/03/2012 3:00 Could not start
    \BackUpTask N/A Ready
    \FacebookUpdateTaskUserS-1-5-21-1343024091-879983540-725345543-1584327Core 29/02/2012 19:16 Ready
    \FacebookUpdateTaskUserS-1-5-21-1343024091-879983540-725345543-1584327UA 29/02/2012 16:16 Ready
    \IDA{07A2D605-F561-11D1-BEE5-AC785AC8CD4E}000 1/03/2012 11:22 Ready

    I am using a monitoring tool , from which i will monitor these log files entry,i am finding difficult while checking the content of "Next Run Time"

     i.e TimeStamp ,where the timestamp is getting change each time.  

    If i put a content search within my monitor template to search for text/string  "The Next Run-time is Disabled" , which can suffice to my need .

    If you have better solution, please suggest?

    Wednesday, February 29, 2012 10:16 AM
  • Well, it was jv's idea to use csv instead of a string (which was my idea), so I'll let him answer that one...

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 29, 2012 10:32 AM
  • Well, it was jv's idea to use csv instead of a string (which was my idea), so I'll let him answer that one...

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Unfortunately I don't understand the question.  Iwas just showing how you can load teh tasks as a CSV.  If you need a specific taks then use teh list format as posted originally.

    Outside of that the question does not make any sense.   Third party tools will need to be figured out with teh vendor as we cannot see or know how these tools work.

    If you just want to know how to output to a file then her eis the answer:

    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo LIST /v > tasklog.txt

    This will redirect output to a fle.

    This does not apper to be a scripting question.  It is more a fundamentalquestion about how to use Windows commands.


    ¯\_(ツ)_/¯

    Wednesday, February 29, 2012 10:45 AM
  • Hello All,

    Can you help me for this part:

    The above command "schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo LIST /v"   results in success and failure output.

    for Success the result is redirected to some output file , but for failure a file with empty is been created.

    Like for Failure: 

    C:\>schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt
    ERROR: The system cannot find the file specified.

    This stage: c:\test.txt is empty.

     

    Here the TaskNamed "Backup1" was not existing in schedule task list , so received "ERROR: The system cannot find the file specified" , Now i need to get the result something like " The Task is been removed from the list" in my redirected file.

    Monday, March 12, 2012 10:15 AM
  • You could catch the errorlevel using batch. Making it look like this:

    @echo off
    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt
    if errorlevel 1 echo ERROR: The system cannot find the file specified. >> c:\test.txt

    Monday, March 12, 2012 10:23 AM
  • You have  to catch it on the error channel.

    schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt 2>>c:\test.txt

    Notice the added 2>>.

    Device 1 is the standard output channel. StdOut.  Device 2 is the standard error channel 'StdErr'.

    Messages go through door #1 and errors goe through door #2.

    Note also that PowerSHell has no support for the error channel.  PowerShell 3 has added this support.


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, March 12, 2012 10:42 AM
    Monday, March 12, 2012 10:42 AM

  • @jrv, for in your case schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt 2>>c:\test.txt ,                                                                   Is it should be : schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt 2>>c:\test2.txt  ???  

                   

    While running the cmd:

    C:\>schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv >>c:\test.txt 2>>c:\test.txt
    The process cannot access the file because it is being used by another process.

    So each time two separate output file will be created?if i use : schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv >>c:\test.txt 2>>c:\test2.txt 

    Please correct me if i am worng?

                    


    • Edited by mj010207 Monday, March 12, 2012 11:40 AM
    Monday, March 12, 2012 11:23 AM
  • Hi Moshi, what do you mean by 'each time i am running the same output file is getting updated with previous run output result, I want to keep the output for recent ran cmd.' I do not quite follow what you are trying to say there.

    About using jrv's method, indeed you need to output to two different logfiles, otherwise you get the error you are receiving.

    Monday, March 12, 2012 11:40 AM

  • @jrv, for in your case schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt 2>>c:\test.txt ,                                                                   Is it should be : schtasks /query /TN "\Microsoft\Windows\TestTask\Backup1" /fo csv >>c:\test.txt 2>>c:\test2.txt  ???  

                   

    While running the cmd:

    C:\>schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv >>c:\test.txt 2>>c:\test.txt
    The process cannot access the file because it is being used by another process.

    So each time two separate output file will be created?if i use : schtasks /query /TN "\Microsoft\Windows\TestTask\Backup" /fo csv >>c:\test.txt 2>>c:\test2.txt 

    Please correct me if i am worng?

                    


    Change >> to >


    ¯\_(ツ)_/¯

    Monday, March 12, 2012 6:23 PM
  • How can i run this with passing variable?

    i.e

    set TaskName1="\Microsoft\Windows\TestTask\Backup"
    set TaskName2="\Microsoft\Windows\TestTask\Scan"

    schtasks /query /TN %TaskName1% %TaskName2% /fo csv

    Above cmd is failing working this way


    Tuesday, March 13, 2012 8:01 AM
  • How can i run this with passing variable?

    i.e

    set TaskName1="\Microsoft\Windows\TestTask\Backup"
    set TaskName2="\Microsoft\Windows\TestTask\Scan"

    schtasks /query /TN %TaskName1% %TaskName2% /fo csv

    Above cmd is failing working this way


    schtasks /query /TN %TaskName1% /fo csv

    You can only have one task name.


    ¯\_(ツ)_/¯

    Tuesday, March 13, 2012 12:35 PM
  • What about doing it this way:

    @echo off


    :: Task Names
    set T1="TestTask\Backup"
    set T2="TestTask\Scan"


    Set tasks=%T1%, %T2%, %T3%
    For %%! in (%tasks%) do (
       START schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv
    )

    Wednesday, March 14, 2012 8:04 AM
  • What about doing it this way:

    @echo off


    :: Task Names
    set T1="TestTask\Backup"
    set T2="TestTask\Scan"


    Set tasks=%T1%, %T2%, %T3%
    For %%! in (%tasks%) do (
       START schtasks.exe /query /TN "\Microsoft\Windows\%%~!" /fo csv
    )

    I suspect by now that yu have discovered that that won't work. If yu jsut execute teh one line it willcreate a file with all tasks status and let you filter it after you have generated the file.

    schtasks.exe /query /fo csv

    In PowerShell we can fo it this way:

    $tasks=(schtasks.exe /query /fo csv)|convertfrom-csv
    $tasks|?{$_.Taskname -like '\\Microsoft\\Windows*'}

    Now you have a set of objects that can be easily manipulated.  The second line filters the result by TaskName.


    ¯\_(ツ)_/¯

    Wednesday, March 14, 2012 5:27 PM