Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to configure a scheduled task.
Microsoft Scripting Guy, Ed Wilson, is here. This morning is a coffee morning. I don’t have a lot of coffee mornings, but occasionally I break down and desire a raw coffee jolt. When I do, I use my French press, some spring water, my rotary bean grinder, and usually some sort of special beans. Usually, that translates into a nice Kona bean. It seems to take me nearly a half hour to make a decent cup of coffee. Luckily, I can almost do it in my sleep—which is a good thing.
Because I do not make much coffee, I feel no need to automate the process. But if I did, I would probably invest in one of those big programmable machines. I would sync it with my alarm clock so the coffee would be available for me when I wake up—sort of like a nice hotel with someone who brings coffee or hot cocoa to your door with your wake-up call.
After a scheduled task is created, there are still many configurable items. Luckily, I can use Windows PowerShell to make these configuration changes. Even better, introduced in Windows 8 and Windows Server 2012, there is the Scheduled Task module that makes these changes a breeze.
Note See yesterday's post, Use PowerShell to Create Scheduled Tasks, for a basic introduction to scheduled tasks and the Scheduled Task module.
Using Windows PowerShell to provide configuration options for a scheduled task is really easy. Basically, there is one cmdlet to create the configuration options and one cmdlet to apply the configuration options (actually, they are advanced functions, not really cmdlets):
The first thing I do is use New-ScheduledTaskSettingsSet to create my configuration options. I store the resulting object in a variable. As shown in the following results, there are many potential options available:
I want to permit my AppLog task (which I created yesterday) to do the following:
- Start if my laptop is on battery power
- Not stop if my laptop transitions to battery
- Start hidden
- Have an execution time limit of five minutes
- Restart three times if something happens to stop the task
Here is the command that does all this (this is a single-line command that wraps on the blog page):
$settings = New-ScheduledTaskSettingsSet –AllowStartIfOnBatteries – DontStopIfGoingOnBatteries -Hidden -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -RestartCount 3
After I create my task settings object, I use Set-ScheduledTask to apply the new settings. The cool thing is that I can apply the new settings even if the scheduled task is currently running. The only catch is that the new settings only take place the next time the task runs. It does not modify the behavior of a currently executing task. Here is the command I use:
Set-ScheduledTask -TaskName applog -Settings $settings
That is it. Two lines of code really. Pretty cool. When I execute the script, it provides a minimal amount of output, but it does not echo the new settings. Here is what the script looks like after it runs.
That is all there is to using Windows PowerShell to configure a scheduled task. Scheduled Task Week will continue tomorrow when I will talk about more cool stuff.
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