Lately I have been having more issues regarding deploying software that depends on scheduled tasks it should create itself/would have to be manually created.
Most of the time we do not want to have to manually create scheduled tasks and the software itself won't always create scheduled tasks when deployed/installed remotely.
So we have to create the scheduled tasks within the Task Scheduler ourselves! And what better way to do it than using Powershell?
In the code block above I have placed the Powershell script I will be describing.
First thing to know is that Register-ScheduledTask is the Powershell command to create a scheduled task. We build around that.
Taskname should be easy to figure out. Just think of one.
Trigger creates the schedule on which the task should run. Choose a time (-at) and an interval.
-DaysInterval X (replace X with the amount of days in between job runs)
-WeeksInterval X (Same as days but weeks instead)
-DaysOfWeek X (Replace X with the names of the days. Example -DaysOfWeek Sunday, Monday)
-Once (only run once)
User sets the user the job runs as. Unless you need to have it run user-specific actions, I prefer to keep it usable in general meaning I use the SYSTEM user.
Action determines what the task should be executing. Whether it is a script or software, it will try to run it. At the argument section you can set any additional arguments.
Runlevel determines whether it uses the highest rights the selected user has. I see no reason to not have this set to the highest.
Force does what it always does. It forces through the creation of the task.
Now you might have seen I skipped one option ... -Settings.
This is because Settings said contains all the other options in a scheduled task.
These are the options that can be added behind the New-ScheduledTaskSettingsSet command.
In my example I have added -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Hours 4) -RunOnlyIfNetworkAvailable.
According to Microsoft these are all options possible:
The best way to identify these options are simply to open a task scheduler creation window and see what options are available there so you can match them with above lists.
This has allowed me to remotely deploy a few software packages that require to be run with a few arguments on a specific schedule without having to manually create scheduled tasks on the device or run the software to schedule it in within.
Hopefully, this was easier to read and execute than Microsoft’s own command pages (as detailed and useful as they might be).
Categories: Powershell, Windows, Script
Patrick Berger AKA Powershellder.
[ i ] Parallax section below. Click on the section below to upload image. Don't worry if it looks weird in the Weebly editor. It'll look normal on your published site.
To edit or delete your image, press the "toggle" button below. Then, hover over your image until a popup appears with the "edit" and "delete" options. If you don't want a white content section, leave it blank. It will disappear on your live website.