= Schedule Tasks on Windows Server =
**Summary**: How to schedule a task on Windows Server 2008 to start a powershell script. \\
**Date**: Around 2010 \\
**Refactor**: 1 May 2025: Checked links and formatting. \\
{{tag>windows powershell}}
The location of the Windows Server 2008 Task Scheduler is a little bit changed. You can find it by starting Server manager -> Configuration -> Task Scheduler. If you created custom tasks you can find them under Task Scheduler -> Task Scheduler Library.
= Creating a Task =
In this example I'll create a task for scheduling a powershell/powercli script as explained in [[powercli-dailycheck]]. Before you start creating the task you'll need an service account which has the following permissions:
* Logon as a batchjob
* Control Panel -> Administrative Tools -> Open Local Security Policy.
* Go to Local Policies -> User Rights Assignments.
* Go to Log on as a batchjob.
* Click Add User or Group, and add the service account to the list of accounts.
* vCenter permissions
* Go to the permissions tab of the vCenter and add the user while assigning read-only rights and propagating this to all child objects.
Then the task:
* Click on Create Task
* In the General tab:
* Give a name and description
* Use the created service account to run the task
* Set the task to "Run whether user is logged on or not"
* Check the "Run with highest privileges
* Configure the task for the correct Operating System (Windows Server 2008 / Windows Server 2008 R2)
* On the Triggers tab create a new trigger based on what time you want the task to start (Daily - 07:30).
* On the Action tab create a new action:
* Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
* Arguments: {{{-PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'C:\Users\Public\Documents\dailycheck.ps1'" vcenter }}}
* On the Settings tab set the task to stop if it runs longer than 1 hour.
Note: On a Windows Server 2008 R2 server and a script which loads the VMware modules itself I had to create the following action:
* Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
* Arguments: {{{-nologo -noninteractive -command "&{D:\Scripts\SRM-Reports\srmreports.ps1}"}}}
When you click OK the task will ask you for the password of the service account you provided in the General tab.
> Arguments on Windows Server 2016 with a script on a remote repository: {{{-Executionpolicy Bypass -nologo -noninteractive -command "& {\\fileserver\dfs\SCRIPT_REPOSITORY$\ICTScripts\script.ps1}"}}}
= Test the Task =
You can test whether the task runs by right-clicking the task and clicking "run".
= Troubleshooting =
* 0x1 - Do not create a batch file for the script but set it up as explained above. \\
* 0x41301 - Actually, the batch file is running. Please wait for the script to finish. \\
* 0x80070569 - The user does not have "logon as a batchjob" permissions. Assign the rights as explained above. \\
* It's also possible to create logging from the scheduled task, to do so, add the following part to the parameter part of the command: {{{ > c:\temp\logfile.txt}}}
* the operator or administrator has refused the request 0x800710e0 - Event 153: Task Scheduler did not launch task "\ICT Control Framework 16 - Management Domain" as it missed its schedule. Consider using the configuration option to start the task when available, if schedule is missed
* Not yet fully fixed, I recreated the tasks and set the new tasks to "Run task as soon as possible after a scheduled start is missed"
== Delete Tasks ==
When troubleshooting you might want to completely remove scheduled tasks. If you want to make sure that a task is completely removed you can check both the filesystem and the registry: \\
Filesystem:
%systemroot%\System32\Tasks
%systemroot%\Tasks
Registry:
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Schedule\Taskcache\Tasks
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Schedule\Taskcache\Tree
> Note that if you recreate the task with the same name, the old history is still displayed. As the ID of the task changes on recreation it probably checks the eventlog based on the name.