none
How to set a script to set schedule tasks?

    Question

  • I would like to set schedule tasks under Window XP SP3 for following conditions, which scripts could be coded under PowerShell or Batch file.

    Run C:\Program\terminal.exe at 9 am on every weekday

    Run C:\Program\terminal.exe at 2:15 pm on every weekday

    Does anyone have any suggestions?

    Thanks in advance for any suggestions


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 2:41 PM

Answers

  • If you prefer using batch files you can also call schtasks.exe. More information can be found here:

    http://technet.microsoft.com/en-us/library/cc725744(v=ws.10).aspx

    This would create the two jobs you gave as an example:

    schtasks /create /tn "Terminal 9am" /tr c:\program\terminal.exe /sc daily /st 09:00:00 /d MON,TUE,WED,THU,FRI
    
    schtasks /create /tn "Terminal 2.15pm" /tr c:\program\terminal.exe /sc daily /st 14:15:00 /d MON,TUE,WED,THU,FRI

    • Proposed as answer by Bigteddy Wednesday, February 15, 2012 3:07 PM
    • Marked as answer by oem7110 Wednesday, February 15, 2012 11:15 PM
    Wednesday, February 15, 2012 3:03 PM
  • In a dos command line with SchTasks yu need to escape the quotes.

    Here is how:

     /tr "\"c:\program\My Folder\terminal.exe\""

    Notice the extra \". Thisis the escaped quote that will be kept in the string when it is used.  It results in a quotes string.  Be sure that yu also have both outer quotes.

    quote slash quote --------- slash quote quote


    ¯\_(ツ)_/¯

    • Marked as answer by oem7110 Friday, February 17, 2012 1:55 AM
    Thursday, February 16, 2012 11:42 PM

All replies

  • Have you tried this method?

    http://blogs.technet.com/b/heyscriptingguy/archive/2004/09/22/how-can-i-manage-scheduled-tasks-using-scripts.aspx

    BR
    Mads


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Wednesday, February 15, 2012 2:47 PM
  • If you prefer using batch files you can also call schtasks.exe. More information can be found here:

    http://technet.microsoft.com/en-us/library/cc725744(v=ws.10).aspx

    This would create the two jobs you gave as an example:

    schtasks /create /tn "Terminal 9am" /tr c:\program\terminal.exe /sc daily /st 09:00:00 /d MON,TUE,WED,THU,FRI
    
    schtasks /create /tn "Terminal 2.15pm" /tr c:\program\terminal.exe /sc daily /st 14:15:00 /d MON,TUE,WED,THU,FRI

    • Proposed as answer by Bigteddy Wednesday, February 15, 2012 3:07 PM
    • Marked as answer by oem7110 Wednesday, February 15, 2012 11:15 PM
    Wednesday, February 15, 2012 3:03 PM
  • WMI would work as suggested by Mads.  You could also look at using the native command-line tools as well:

    AT
    Schtasks

    I would've posted links but copy/paste isn't working for me at the moment.  Just google "at technet", "at scheduled tasks", "schtasks" to get the msdn/technet/microsoft articles describing their usage.

    Wednesday, February 15, 2012 3:07 PM
  • I find following coding, but get no idea on how to modify and apply it for my case.

    Do you have any suggestions?

    Thanks you very much for any suggestions

    strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objNewJob = objWMIService.Get("Win32_ScheduledJob") errJobCreated = objNewJob.Create _ ("Cleanup.exe", "********123000.000000-420", _ True , 1 OR 4 OR 16, , , JobID) Wscript.Echo errJobCreated

    The preceding script schedules a hypothetical program named Cleanup.exe to run at 12:30 PM every Monday, Wednesday, and Friday.


    Thanks in advance for any suggestions


    • Edited by oem7110 Wednesday, February 15, 2012 3:24 PM
    Wednesday, February 15, 2012 3:24 PM
  • Similarly, you can use the AT command in Powershell, which has the advantage of being able to set a task on a remote machine.  To set this task on multiple machines, you could use a script like this:

    foreach ($computer in (Get-Content $computers)) {
        at \\$computer 9:00 /every:M`,T`,W`,Th`,F C:\Program\terminal.exe
        at \\$computer 14:15 /every:M`,T`,W`,Th`,F C:\Program\terminal.exe
        }


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 15, 2012 3:25 PM
  • I get following error, when I run it on my computer, if I would like to set this task only on my local machines, do you have any suggestions on how to modify the code?

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 3:41 PM
  • Just like this:

    at \\$computer 9:00 /every:M`,T`,W`,Th`,F C:\Program\terminal.exe
    at \\$computer
    14:15 /every:M`,T`,W`,Th`,F C:\Program\terminal.exe


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 15, 2012 3:51 PM
  • When I run it, it requires to activate schedule service first, do you have any suggestions?

    Furthermore, when there is space on path, do I need to apply quote shown as following?

    Does anyone have any suggestions?

    Thanks everyone very much for any suggestions

    at \\$computer 9:00 /every:M`,T`,W`,Th`,F "C:\Program\My Folder\terminal.exe"
    at \\$computer
    14:15 /every:M`,T`,W`,Th`,F "C:\Program\My Folder\terminal.exe"


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 4:11 PM
  • Hi,

    It seems you are really asking questions about how to schedule tasks?

    I would suggest you first learn how to schedule a task on one computer. Once that is working, you can then expand that knowledge to make it work on multiple computers.

    Bill

    Wednesday, February 15, 2012 4:15 PM
  • Use this code.  No, do not put the program path in quotes.

    $computer = 'localhost' net start "Task Scheduler" at \\$computer 9:00 /every:M`,T`,W`,Th`,F C:\Program\My Folder\terminal.exe

    at \\$computer 14:15 /every:M`,T`,W`,Th`,F C:\Program\My Folder\terminal.exe



    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Wednesday, February 15, 2012 4:24 PM
    Wednesday, February 15, 2012 4:23 PM
  • I get following massages,

    Already Start required service

    Detailed option, please input NET HELPMSG 2182.

    The value within option is not correct.

    AT command ....

    Do you have any suggestions on this error?
    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions


    • Edited by oem7110 Wednesday, February 15, 2012 4:29 PM
    Wednesday, February 15, 2012 4:29 PM
  • Hi,

    Please ask about it in a general Windows forum.

    Bill

    Wednesday, February 15, 2012 4:33 PM
  • Will it be a script question? a long time ago, I have tried asked some script question about window on Windows forum before, but they redirect me to ask any related script question on this forum.

    Do you have any suggestions?

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions



    • Edited by oem7110 Wednesday, February 15, 2012 4:39 PM
    Wednesday, February 15, 2012 4:37 PM
  • I agree with Bill.  If you are not going to run this against multiple machines, then why script it at all?  A task only has to be created once.  You could have created your scheduled tasks 10 times over in the time you've taken on this question.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 15, 2012 4:38 PM
  • Because I need to restore my window very often, so everytime, after I restore my window, and need to reset schedule again and again. This script will help me a lot for this managing task.

    Does anyone have any suggestions?

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 4:41 PM
  • What isn't working for you right now, AT and Schtasks seem to be perfect for what you require. Can you explain where you are stuck now?  
    Wednesday, February 15, 2012 4:48 PM
  • The simplest would be the batch file option.  Take either JBrasser's code or this, and stick it in a batch file.

    net start "Task Scheduler"
    at \\localhost 9:00 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe
    at \\localhost 14:15 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Wednesday, February 15, 2012 4:55 PM
    Wednesday, February 15, 2012 4:54 PM
  • Please see following image for detailed error on each step.

    Does the code miss the name for this scheduled task? when I manually set a schedule task, I need to give a name for this task, but I see no name for following task.

    Does anyone have any suggestions?

    Thanks everyone very much for any suggestions

    net start "Task Scheduler"
    at \\localhost 9:00 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe
    at \\localhost
    14:15 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 5:03 PM
  • The AT command doesn't take a name parameter, it just called the tasks At1, At2, etc.  That is a disadvantage of this method.  I'm not sure about shchtasks.

    You keep asking for suggestions, and my suggestion remains that you use a batch file, not Powershell. 


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 15, 2012 5:12 PM
  • I run it on batch file, it seems to me that Task Scheduler cannot start under batch file.

    Do you have any suggestions?

    Thanks everyone very much for any suggestions

    net start "Task Scheduler"
    at \\localhost 9:00 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe
    at \\localhost 14:15 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 5:18 PM
  • Run with elevated priveliges (as Administrator).

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 15, 2012 5:20 PM
  • I am assigned as Administrator.

    Do you have any suggestions?

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions

    Wednesday, February 15, 2012 5:26 PM
  • Hi,

    Also:

    at \\localhost 9:00 /every:M,T,W,Th,F C:\Program\My Folder\terminal.exe

    This command won't work because the program to excute contains spaces in its name and it is not enclosed in quotes. It should be

    at \\localhost 9:00 /every:M,T,W,Th,F "C:\Program\My Folder\terminal.exe"

    Bill

    • Proposed as answer by Bigteddy Wednesday, February 15, 2012 6:57 PM
    Wednesday, February 15, 2012 5:48 PM
  • I try it, but still get the same error as shown from above testing.

    Something is wrong with "/EVERY:m,t,w,th,f", when I remove it, it works.

    Working sample

    at \\localhost 9:00 /every:1 "C:\Program\My Folder\terminal.exe"

    Not working sample

    at \\localhost 9:00 /every:M,T,W,Th,F "C:\Program\My Folder\terminal.exe"

    Does anyone have any suggestions?
    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions



    • Edited by oem7110 Wednesday, February 15, 2012 10:55 PM
    Wednesday, February 15, 2012 10:24 PM
  • Hi,

    I can't read your language, sorry. You will need to ask your question in a Windows support forum in your language.

    Bill

    Wednesday, February 15, 2012 10:26 PM
  • Hi,

    I can't read your language, sorry. You will need to ask your question in a Windows support forum in your language.

    Bill

    Since I am so good at cyphers I was able to decode the message:

    C:\Program Files\Windows Resource Kits\Tools>net helpmsg 2182

    The requested service has already been started.

    EXPLANATION

    You tried to start a service that is already running.

    ACTION

    To display a list of active services, type:

            NET START

    Bill's previous message is the reason for the other errors.

    'at' cannot schdule certain things because it runs at too low a privilege level.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, February 15, 2012 10:42 PM
    Wednesday, February 15, 2012 10:40 PM
  • Also th eformat is wromin two other respects.

    This:
    at \\localhost 9:00 /every:M,T,W,Th,F "C:\Program\My Folder\terminal.exe"

    Should be:

    at 9:00 /every:"M,T,W,Th,F" "C:\Program\My Folder\terminal.exe"

    1. you cannot use \\localhost  this only works with remote machines
    2. You must quote out the M,T,W, string because commas ae delimiters on the copmmand line just like spaces.  Thsy need quotes.

    Most programs will not run under the AT scheduler.   You should use SchTasks to run this. (JBrasser post above)

    This has nothing to do with scripting.  It is about basic WIndows operations and should be posted in teh Windows new users forum for XP.


    ¯\_(ツ)_/¯

    Wednesday, February 15, 2012 10:49 PM
  • schtasks /create /tn "Terminal 9am" /tr c:\program\terminal.exe /sc weekly /st 09:00:00 /d MON,TUE,WED,THU,FRI

    It works now.

    Thanks everyone very much for suggestions


    Thanks in advance for any suggestions


    • Edited by oem7110 Wednesday, February 15, 2012 11:13 PM
    Wednesday, February 15, 2012 11:12 PM
  • schtasks /create /tn "Terminal 9am" /tr "c:\program\My Folder\terminal.exe" /sc weekly /st 09:00:00 /d MON,TUE,WED,THU,FRI

     When I run above script, it works on creating schedule task, but this task cannot be run, because of missing quote of the path, which show c:\program\My Folder\terminal.exe only without quote.

    Does anyone have any suggestions on how to add quote on above coding? so it would create the path and display

    "c:\program\My Folder\terminal.exe" instead of c:\program\My Folder\terminal.exe within the path of schedule task.

    Thanks everyone very much for any suggestions


    Thanks in advance for any suggestions

    Thursday, February 16, 2012 11:20 PM
  • schtasks /create /tn "Terminal 9am" /tr "c:\program\My Folder\terminal.exe" /sc weekly /st 09:00:00 /d MON,TUE,WED,THU,FRI

    Change
    "c:\program\My Folder\terminal.exe"
    to
    """"c:\program\My Folder\terminal.exe""""
    Thursday, February 16, 2012 11:28 PM
  • In a dos command line with SchTasks yu need to escape the quotes.

    Here is how:

     /tr "\"c:\program\My Folder\terminal.exe\""

    Notice the extra \". Thisis the escaped quote that will be kept in the string when it is used.  It results in a quotes string.  Be sure that yu also have both outer quotes.

    quote slash quote --------- slash quote quote


    ¯\_(ツ)_/¯

    • Marked as answer by oem7110 Friday, February 17, 2012 1:55 AM
    Thursday, February 16, 2012 11:42 PM
  • Thanks everyone very much for suggestions

    Thanks in advance for any suggestions

    Friday, February 17, 2012 1:56 AM
  • In a dos command line with SchTasks yu need to escape the quotes.

    "DOS command"? I thought that we saw the last of DOS with WinME? Or do you refer to every black command console as "DOS", including those included with Unix? ;-)
    Friday, February 17, 2012 6:54 AM
  • "DOS command"? I thought that we saw the last of DOS with WinME? Or do you refer to every black command console as "DOS", including those included with Unix? ;-)


    If I remember correctly, DOS didn't have FOR.  I'm not sure that much else has changed from DOS to cmd.exe.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, February 17, 2012 7:37 AM
  • I also think cmd.exe needs a better name.  That's probably why jv called it DOS.  What do you call it? 

    Batch?  Well, that's not very specific, batch is a generic term for a set of commands.

    Command? Ditto.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, February 17, 2012 7:40 AM
  • I also think cmd.exe needs a better name.  That's probably why jv called it DOS.  What do you call it? 

    Batch?  Well, that's not very specific, batch is a generic term for a set of commands.

    Command? Ditto.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    "cmd.exe" is usually referred to as the "Command Processor" because it processes commands in a console environment.

    Batch files, as you suggest, started off as a collection of console commands such as copy, cd, del. Over the various versions of DOS and Windows they acquired a limited degree of intelligence and flexibility such as the "for" command or the ability to accept user input. Because most of these add-ons were not initially planned, batch files lack elegance and robustness. They are highly effective for simple tasks and about as graceful as a wart hog.


    Friday, February 17, 2012 9:25 AM
  • Who remembers NDOS?  Norton DOS.  This was a replacement/shell that ran on top of DOS, but replaced command.exe.  It gave one a great deal more "intelligence" as you call it.  If I remember correctly, you could solicit user input and all sort of things, that the DOS of the day lacked.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, February 17, 2012 12:44 PM
  • In a dos command line with SchTasks yu need to escape the quotes.

    "DOS command"? I thought that we saw the last of DOS with WinME? Or do you refer to every black command console as "DOS", including those included with Unix? ;-)

    Shh!  I was trying to be nice.


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 2:26 PM
  • I also think cmd.exe needs a better name.  That's probably why jv called it DOS.  What do you call it? 

    Batch?  Well, that's not very specific, batch is a generic term for a set of commands.

    Command? Ditto.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    'Command shell', CLI, "The Original Legacy App".

    Ther eis not good name. 

    It has traditionally been called teh 'Console' but now we have Powershell whcich can be called a console.   It i sgood tehat PowerSHell actually has a name.

    Ar least in Unix we have names for all of the shells.


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 2:29 PM
  • Who remembers NDOS?  Norton DOS.  This was a replacement/shell that ran on top of DOS, but replaced command.exe.  It gave one a great deal more "intelligence" as you call it.  If I remember correctly, you could solicit user input and all sort of things, that the DOS of the day lacked.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    4DOS, DoubleDos, DOS-TO-GO,

    We have had them all.

    ALl of those DOS switchers that synthsized multitasking.

    What about the biggest DOS of them all - OS2.

    Remember.  Install OS2 from 30+ disks.  NOw install Windows 1.0 into OS2 so you have a GUI.

    OS2 was just multitasking DOS hosting a copy of Windows.


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 2:33 PM
  • Look at me, calling it command.exe!  It was called command.com, and I should remember, I've seen enough missing Command.com problems.  How time flies.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Edit: Do .com files still run in Windows?

    • Edited by Bigteddy Friday, February 17, 2012 2:43 PM
    Friday, February 17, 2012 2:42 PM
  • NDOS was modified version of 4DOS, which was a replacement for command.com on MS-DOS/Windows 9x. I believe 4DOS is now available for free.

    Maybe I'm a bit pedantic, but it bothers me when the cmd.exe command window gets called "DOS". DOS was an operating system, not a command shell. The term "batch file" is a bit antiquated also; I prefer "shell script" (a more accurate description). Just my $0.02.

    Bill

    Friday, February 17, 2012 3:14 PM
  • I prefer "shell script" (a more accurate description). Just my $0.02.

    Bill

    You see, that's my point:  It lacks a name.  It's not "Powershell" script, it's "        shell" script.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Edit:  I'd fill the blanks in with "Command".  Hence "CommandShell script".


    • Edited by Bigteddy Friday, February 17, 2012 3:26 PM
    Friday, February 17, 2012 3:24 PM
  • I prefer "shell script" (a more accurate description). Just my $0.02.

    Bill

    You see, that's my point:  It lacks a name.  It's not "Powershell" script, it's "        shell" script.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Edit:  I'd fill the blanks in with "Command".  Hence "CommandShell script".


    Yes - PowerShell can also be easily called a 'shell' as well as 'console' or CLI.

    DOS has always been understood by all as incorrect as it may be.

    It desiplay by default as 'COmmand Shell' but now that can be eaily applied to PowerShell.

    MS support and others always say 'go to a command prompt'.  Today I would end up ata PowerSHell prompt if that is asked of me.

    The shell in NT3.1 through W2K dispalyed on the desktop as a DOS icon.

    Command.com stillexisits on WS2003 and is tagged in the system as MS-DOS Aplication even though it just launches CMD.EXE.

    I am in favor of calling a DOS a DOS.

    Or .. "A DOS  by any other name..."

    Or ... "If it looks like a DOS and it quacks like a DOS it must be..."


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 3:49 PM