Learn about Windows PowerShell
Summary: Learn about using scheduled tasks and scheduled jobs in Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. Today Dave Bishop, senior technical writer on the Windows Server team, and June Blender, senior programming writer on the Windows Azure Active Directory team, investigate scheduled tasks and scheduled jobs in Windows PowerShell. And now, here’s June…
Windows PowerShell 3.0 introduced dozens of new modules and thousands of new cmdlets. Much of this was the work of Microsoft teams building Windows PowerShell interfaces for Windows tools. In the mix were two modules that sound like they do almost the same thing: PSScheduledJob and ScheduledTasks. They're actually different approaches to solving the same problem, and they share some common technology. The best one to use depends on what you're trying to schedule.
Dave will start us out with a review of scheduled tasks, and I'll jump in with some info about scheduled jobs. Be sure to read the official help topics for both modules: PSScheduledJob Module and Scheduled Task Cmdlets in Windows PowerShell.
Scheduled tasks are much more general than scheduled jobs. The Task Scheduler graphical UI program (TaskSchd.msc), and its command-line equivalent (SchTasks.exe) have been part of Windows since some of the earliest days of the operating system. They enable you to schedule the running of almost any program or process, in any security context, triggered by a timer or a wide variety of system events.
Scheduled tasks are a core infrastructure component of Windows and, they are used extensively by many Windows components and other products that run on Windows. Although SchTasks.exe enables you to fully interact with your tasks from a command line, it is not object-oriented, as are the ScheduledTask module’s cmdlets. Each cmdlet consumes, processes, or returns an object, unlike the older command-line tools that generally consume and emit only text strings.
The only thing that is really missing from a scheduled task is the native ability to capture and manipulate the output of the task. Because a scheduled task can run almost anything that is runnable on a Windows computer, it’s not possible to anticipate and capture the output of a scheduled task. According to Windows PowerShell MVP, Karl Prosser, the output of scheduled tasks goes to "the same place where your clothes dryer puts your lost socks."
Because a scheduled job is always Windows PowerShell script, even if that script is used to run a non-Windows PowerShell program, it is possible for the system to capture that predictable output: the Windows PowerShell object that is returned at the end of the script block. However, that’s a relatively minor limitation because there is nothing to prevent your scheduled task from saving its output to disk in some form that you can later retrieve and manipulate.
A scheduled task consists of:
You can find the cmdlets that work with scheduled tasks in the ScheduledTasks module that is included with Windows 8 and Windows Server 2012. To see the complete list of cmdlets on your system, run the command:
Get-Command –Module ScheduledTasks
Hint You can also abbreviate that to gcm –m ScheduledTasks. To see the syntax and description for any cmdlet, run Get-Help CmdletName. You can see more details and examples if you use the -Detailed, -Examples, or -Full parameters. The cmdlets let you define, view, and modify scheduled task objects and all the component objects that make up a scheduled task. For example, the commands below create a scheduled task that launches Notepad as an Administrator at 5:00 AM. I know, I know, unbelievably useful.
$ST_A = New-ScheduledTaskAction –Execute “c:\windows\system32\notepad.exe” $ST_T = New-ScheduledTaskTrigger -Once -At 5am Register-ScheduledTask –TaskName “Notepad As Admin at 5AM” -Action $ST_A –Trigger $ST_T –User “Contoso\Administrator”
One other important feature about scheduled tasks that is not shared by scheduled jobs is the ability to deal with the specifics of tasks on a failover cluster server. Tasks on a cluster server can be specified to run as follows:
These capabilities add some power to scheduled tasks in the failover cluster environment. There are no cluster-specific options for scheduled jobs. Now let’s switch over to June for a look at some other interesting intersections of these two technologies.
Thanks, Dave! As Dave explained, scheduled tasks are native to Windows. Similarly, scheduled jobs are native to Windows PowerShell. They're a hybrid of background jobs and scheduled tasks. Scheduled jobs added the ability to schedule a background job, just like you schedule a task.
In addition to using the job and scheduled job cmdlets, you can use the Task Scheduler UI and scheduled task cmdlets to manage scheduled jobs, but you can't use the job or scheduled job cmdlets to manage scheduled tasks. Scheduled jobs and scheduled tasks export their definitions as .xml files, but the files contain different data and use a different XML schemas, so they're not fully compatible.
There are a few similarities. The scheduling features of scheduled jobs are modeled on scheduled tasks. The design team tried to enable the same job options that you find in Task Scheduler, and they used the same terminology. To start a scheduled job (or a scheduled task), you use a "trigger." The commands that run are called "actions." And they tried to tie scheduled jobs to Task Scheduler as much as they could.
To create a scheduled job, you have use the scheduled job cmdlets. (Anything created in Task Scheduler is a scheduled task, even if it's in the ScheduledJobs path.) But after the scheduled job is created, you can view and manage it in Task Scheduler. Scheduled jobs are located in the Microsoft\Windows\PowerShell\ScheduledJobs path in Task Scheduler. Here's a screenshot of my Windows PowerShell console superimposed on Task Scheduler.
If you select a scheduled job in Task Scheduler, you can see the job triggers on the Triggers tab, and you can see the scheduled job options on the General tab and on the Conditions tab. Job instances that have run are shown on the History tab.
If you change a scheduled job setting in Task Scheduler, it is effective for all future instances of the scheduled job. For example, if you change the trigger time, you can see the new time in Windows PowerShell. The change affects all future instances of the job. For example, if I use Task Scheduler to change the trigger time from 10:20 AM to 9:20 AM, I can see it in Windows PowerShell, and it’s effective—future instances run at 9:20 AM.
PS C:\ps-test> Get-ScheduledJob Update-MyHelp | Get-JobTrigger
Id Frequency Time DaysOfWeek Enabled
-- --------- ---- ---------- -------
1 Daily 10/17/2012 9:20:00 AM True
You can even change a value on the Settings tab to add a setting that isn't available in the scheduled job cmdlets. My favorite value is Run the task as soon as possible if a scheduled start is missed.
There are some cool things in scheduled jobs that aren't available in scheduled tasks. The job instances that the scheduled jobs start are Windows PowerShell background jobs that you can view and manage by using the Job cmdlets. The best part of a scheduled job is that all results of all job instances are saved at the following location, and they are available to you at any time:
To get them, run:
PS C:\ps-test> dir $home\appdata\local\Microsoft\Windows\PowerShell\ScheduledJobs\Update-MyHelp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 10/14/2013 10:20 AM Output
-a--- 10/15/2013 2:50 PM 7000 ScheduledJobDefinition.xml
The scheduled job action (the command that runs when the job is triggered) can be Windows PowerShell commands in a script block, Windows PowerShell scripts (identified by file path), or any executable file on the machine.
Hey, Dave, can I use SchTasks.exe or the scheduled task cmdlets on my scheduled job?
You sure can, June! You can use the UI, SchTasks.exe, or the cmdlets in the ScheduledTasks module to modify the triggers and several other settings of the job. The biggest thing to notice is that running the script is automatically translated in the task to run PowerShell.exe with the hidden parameters –NoLogo, –NonInteractive, and –WindowStyle, and with an the following embedded Windows PowerShell script:
Import-Module PSScheduledJob $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('JobName', 'C:\Users\UserName\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs') $jobDef.Run()
As you can see, the script loads the needed PSScheduledJob module, invokes the static LoadFromStore procedure from the Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition class, passes it the name and path of the job, stores it in a variable named $jobDef, and runs the job in that variable.
Because Task Scheduler doesn’t know about the vagaries of Windows PowerShell, the scheduled job cmdlets automatically add all of the Windows PowerShell details that the Task Scheduler needs. This detail is included in the Scheduled Job in the PSExecutionPath and PSExecutionArgs properties.
Here are the property values from June’s Update-MyHelp scheduled job:
PS C:\> (Get-ScheduledJob Update-MyHelp).PSExecutionArgs
-NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('Update-MyHelp', 'C:\Users\juneb\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"
PS C:\ > (Get-ScheduledJob Update-MyHelp).PSExecutionPath
Because of the lack of knowledge of Windows PowerShell internals, the Scheduled Task tools don’t know about the output results of the scheduled jobs. There are no scheduled task cmdlets that enable you to access those run instances the way you can with the Get-Job cmdlet.
That's great, Dave. When you use the scheduled task cmdlets, note that they use the Task Scheduler path and name to identify a task, not just the task name.
So, when you use the scheduled task cmdlets to get or set a scheduled job, you might need to provide the full Task Scheduler path. Fortunately, it's easy to get, because it's the value of the TaskPath property of the task.
For example, this command sequence, which sets a new value for a task setting, fails without the task path:
PS C:\> $s = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter (New-TimeSpan -Days 14)
PS C:\> Set-ScheduledTask Update-MyHelp -Settings $s
Set-ScheduledTask : The system cannot find the file specified.
At line:1 char:1
+ Set-ScheduledTask Update-MyHelp -Settings $s
+ CategoryInfo : ObjectNotFound: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Set-ScheduledTask]
+ FullyQualifiedErrorId : HRESULT 0x80070002,Set-ScheduledTask
But it succeeds when you add the TaskPath parameter:
PS C:\> $path = (Get-ScheduledTask Update-MyHelp).TaskPath
PS C:\> Set-ScheduledTask Update-MyHelp -Settings $s -TaskPath $path
The scheduled job cmdlets expect the tasks to be in the path Task Scheduler located at: Library/Microsoft/Windows/PowerShell/ScheduledJobs
Tasks stored in any other path are not accessible from the scheduled job cmdlets because there is no Path parameter to enable you find tasks in other paths. All scheduled jobs are created in that path, so they are expected to be there. If you need to manipulate a task in any other path, you must use the scheduled task tools and cmdlets to do it.
If you created a scheduled task by any means other than the scheduled job cmdlets, the task is likely stored somewhere other than the PowerShell/ScheduledJobs folder. If that is the case, you must use the scheduled tasks tools and cmdlets because the task simply is not accessible to the scheduled job cmdlets.
So the big question is: which do I use? A scheduled task or a scheduled job? You can use either for most tasks. However, remember that a lot of Windows PowerShell details are exposed in the Scheduled Tasks view of a job to handle the Windows PowerShell specifics that scheduled job cmdlets handle in the background.
If you're running a Windows executable that is logging to a file or not generating output objects, or if you don't care about the output, running the tool is probably the easiest way to create a scheduled task. Also, if the task has no need for Windows PowerShell and the overhead that running it entails, you’re better off with a scheduled task. And if you're managing failover clusters, definitely use a scheduled task.
But if you're running Windows PowerShell commands or scripts, or Windows tools that don't write their output to a file, and the output is important, a scheduled job is the better choice.
The cool part is that, in Windows PowerShell 4.0 and Windows PowerShell 3.0, if you need to create multiple scheduled jobs or tasks on the local computer or on remote computers, you can automate their creation and maintenance with the Scheduled Job or the scheduled task cmdlets.
~June and Dave
Thanks for the great post! I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Hi the help for Register-ScheduledTask cmdlet in Synopsis says
Registers a scheduled task definition on a local computer.
But we can register shceduled tasks on a remote machine using CIMSession , right ?
I have a script to scan all computers in AD and log Excel sheet. The script work fine when it run alone. But won't log / create excel sheet when I schedule it run under schedule task / schedule job either.
Any ideas would help?
Good Wrap up
>>The cool part is that, in Windows PowerShell 4.0 and Windows PowerShell 3.0,
well to be more precise: it is as of Windows 8 you can use also ScheduledTasks cmdlets.
In powershell 3 on Windows 7 it is not available.
Below Power shell script can be sued for monitoring of scheduled tasks, we are using it & it works finehttp://msexchange.me/2013/12/22/schedule-task-monitor-script/
What about using scheduled tasks cmdlets in core or hyper-v servers?
[On a specific resource: There is only one instance of the task, and it is bound to a resource.]
Exactly how do you bind the task to a specific resource? E.g. a specific disk to run on the node that owns the disk?