{"id":71383,"date":"2004-09-22T13:32:00","date_gmt":"2004-09-22T13:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/09\/22\/how-can-i-manage-scheduled-tasks-using-scripts\/"},"modified":"2004-09-22T13:32:00","modified_gmt":"2004-09-22T13:32:00","slug":"how-can-i-manage-scheduled-tasks-using-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-manage-scheduled-tasks-using-scripts\/","title":{"rendered":"How Can I Manage Scheduled Tasks Using Scripts?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Hey, Scripting Guy! Is there an easy way to manage scheduled tasks using scripts?<\/p>\n<p>&#8212; RC<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/a><\/p>\n<p>Hey, RC. You just <i>had<\/i> to ask that question, didn\u2019t you? To be honest, this is not one of scripting\u2019s strong points, and you might be more confused after hearing the answer than you were before asking the question. But here goes.<\/p>\n<p>The problem we have here is that Windows actually has two different &#8211; and, sadly, not fully-compatible &#8211; APIs (Application Programming Interfaces) used to manage scheduled tasks. For one, there are the Task Scheduler APIs used by both the Task Scheduler and by Schtasks.exe, a command-line task management tool that ships with Windows XP and Windows Server 2003. For another, there are the so-called At APIs used by At.exe and by WMI\u2019s Win32_ScheduledJob class.<\/p>\n<p>So what\u2019s the problem? Well, let\u2019s say you use WMI to create a scheduled task (and we\u2019ll show you a sample script for doing so in a minute). If you do that, you can then use a script similar to this to retrieve information about that task and about any other tasks created using the At APIs:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\" _\n    &amp; \"{impersonationLevel=impersonate}!\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colScheduledJobs = objWMIService.ExecQuery _\n    (\"Select * from Win32_ScheduledJob\")\nFor Each objJob in colScheduledJobs\n    Wscript.Echo \"Caption: \" &amp; objJob.Caption\n    Wscript.Echo \"Command: \" &amp; objJob.Command\n    Wscript.Echo \"Days Of Month: \" &amp; objJob.DaysOfMonth\n    Wscript.Echo \"Days Of Week: \" &amp; objJob.DaysOfWeek\n    Wscript.Echo \"Description: \" &amp; objJob.Description\n    Wscript.Echo \"Elapsed Time: \" &amp; objJob.ElapsedTime\n    Wscript.Echo \"Install Date: \" &amp; objJob.InstallDate\n    Wscript.Echo \"Interact with Desktop: \" &amp; objJob.InteractWithDesktop\n    Wscript.Echo \"Job ID: \" &amp; objJob.JobID\n    Wscript.Echo \"Job Status: \" &amp; objJob.JobStatus\n    Wscript.Echo \"Name: \" &amp; objJob.Name\n    Wscript.Echo \"Notify: \" &amp; objJob.Notify\n    Wscript.Echo \"Owner: \" &amp; objJob.Owner\n    Wscript.Echo \"Priority: \" &amp; objJob.Priority\n    Wscript.Echo \"Run Repeatedly: \" &amp; objJob.RunRepeatedly\n    Wscript.Echo \"Start Time: \" &amp; objJob.StartTime\n    Wscript.Echo \"Status: \" &amp; objJob.Status\n    Wscript.Echo \"Time Submitted: \" &amp; objJob.TimeSubmitted\n    Wscript.Echo \"Until Time: \" &amp; objJob.UntilTime\nNext\n<\/pre>\n<p>On top of that, if you open up the Task Scheduler, you\u2019ll see the task in there as well. So far so good, huh?<\/p>\n<p>However, suppose you say to yourself, \u201cHey, as long as I have the Task Scheduler open I might as well schedule a second task.\u201d Let\u2019s say you do so. That task, created by the Task Scheduler, will <i>not<\/i> be visible to your WMI scripts. The Task Scheduler can deal with tasks created by WMI, but WMI cannot deal with tasks created by the Task Scheduler. <\/p>\n<p>Even worse, suppose you use Task Scheduler to modify the task you originally created with WMI. That task will be successfully modified, but you\u2019ll no longer be able to access it using WMI, even though WMI created the thing in the first place. Yes, we know. And, hopefully, this problem will be corrected in future versions of Windows. And, yes, we know: that doesn\u2019t really help you much right now, does it? Sorry.<\/p>\n<p>So what does all this mean? Well, it means that managing scheduled tasks using WMI is kind of a hit-or-miss proposition. If you can be assured that no one is scheduling tasks using the Task Scheduler then WMI works great. But if people are using both WMI scripts and the Task Scheduler, well, then management becomes a bit more complicated, to say the least. <\/p>\n<p>So how <i>do<\/i> you create a scheduled task using a script? Try this example for starters:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\" _\n    &amp; \"{impersonationLevel=impersonate}!\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet objNewJob = objWMIService.Get(\"Win32_ScheduledJob\")\nerrJobCreated = objNewJob.Create _\n    (\"Cleanup.exe\", \"********123000.000000-420\", _\n        True , 1 OR 4 OR 16, , , JobID)\nWscript.Echo errJobCreated\n<\/pre>\n<p>A bit cryptic, isn\u2019t it? The preceding script schedules a hypothetical program named Cleanup.exe to run at 12:30 PM every Monday, Wednesday, and Friday. For more information about the different parameters used in this script, you might want to check out this section of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_man_lpja.mspx\">Microsoft Windows 2000 Scripting Guide<\/a>.<\/p>\n<p><br><\/p>\n<div>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"\"><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0922.mspx#top\"><img decoding=\"async\" height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/a><a class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0922.mspx#top\">Top of page<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there an easy way to manage scheduled tasks using scripts? &#8212; RC Hey, RC. You just had to ask that question, didn\u2019t you? To be honest, this is not one of scripting\u2019s strong points, and you might be more confused after hearing the answer than you were before asking the question. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,32,3,5],"class_list":["post-71383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scheduled-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there an easy way to manage scheduled tasks using scripts? &#8212; RC Hey, RC. You just had to ask that question, didn\u2019t you? To be honest, this is not one of scripting\u2019s strong points, and you might be more confused after hearing the answer than you were before asking the question. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}