The more parameters are added, the more complex the manual creation of automatic timed-controlled tasks in Windows are. In the first part of my blog posting, I showed how to use the Windows Task Scheduler to run PowerShell scripts at specific times.
However, if you want to create several, similar tasks that differ only in some parameters you may work differently. Microsofts provided Wizards is not the most efficient way to proceed. I will show you a way to create multiple scheduled tasks quickly and without a wizard.
Index
Accelerate tasks with PowerShell
Once again, you can use PowellShell to speed up task creation. The following PowerShell cmdlets are available:
- New-ScheduledTaskAction, to indicate which program to execute
- New-ScheduledTaskPrincipal, to specify the user under which the task should be run
- New-ScheduledTaskTrigger, to determine when something is executed
- New-ScheduledTask, to aggregate the task
- Register-ScheduledTask, to add this to the Task Scheduler
Here is an example script for the above use case:
1 2 3 4 5 6 7 |
$argument = "-Command `"& 'C:\temp\TestScript.ps1' -p1 'hallo' -p2 'Welt'`"" $action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument $argument $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest $trigger = New-ScheduledTaskTrigger -AtStartup $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger Register-ScheduledTask "ExampleTask" -InputObject $task -Force |
The result should look like this:
Just adjust the script to create multiple scheduled tasks quickly.
Restrictions you should considered
This task creation script can only be run as an administrator!
The string following “-Command” in the $ argument variable must be “escaped” for PowerShell. This can be achieved by using `<string>.
Individual configuration of a task
There are many ways to adjust the configuration of a task. Both are possible through the wizard and through the various cmdlet parameters. These are very well documented. It is important not to select “Create Basic Task” but “Create Task”.
Conclusion
For a single time-controled automation tasks, Windows task scheduling with the help of PowerShell is a very good option. However, manually creating these tasks can be really time-consuming. It also gets confusing in the wizard as soon as a PowerShell script has many parameters.
Remedy the fact that you do not actually need the Wizard, but can create multiple scheduled tasks via PowerShell. Here you have to make sure that the syntax of the argument is correct. Once you’ve done that, creating more tasks like this is no longer a problem. I hope this blog post helps overcome this initial hurdle. If you want to go forward with Powershell, you should have a look at our Recursive list of group members in AD or Import PowerShell sessions from computers in another domain article.
FirstAttribute AG – Identity Management & IAM Cloud Services
We would be happy to present our services and solutions to you. Get in touch and find out how we can help you.
Leave a Reply
<p>Your email is safe with us.<br/>Information about our <a href="https://activedirectoryfaq.com/contact-us/">data protection policies</a></p>