Managing Windows Scheduled tasks in a database - Beta Version
Over time more and more tasks accumulate in my Windows Scheduled Tasks library. When moving to a new computer or setting up a new OS installation, I needed a way to copy the tasks over.
You can export the tasks manually into XML files and then re-import them on a different machine, but you still need to set the user account and the password for the task.
The schtasks.exe command line tool allows you to export all tasks and import them again, but it does not work on tasks that are in folders. It can only create tasks in the root of the library.
Kotor is a small basic tool that reads all the tasks on one machine and puts the information into a database. You can then edit the task data and use Kotor to create new tasks on the same or a different machine.
KotorCli.exe -command -i = imports all tasks on the current machine. Deletes all existing database entries for the machine first. -help = show help for all available options.
Currently the command line tool only supports operations on the local computer.
Over time you create new tasks, but you forget to update the Kotor database, so run this tool once a day to make sure the tasks library and the database are always in sync.
Of course you use Task Scheduler itself to run LotorCli.exe once a day.
The configuration file 'KotorCli.exe.config' has two options you can change:
TopLevelFolderFilter allows you to set a regular expression to match the name of the top level folders to import. Say you put all your tasks in a folder names 'Acme', then put 'Acme' as the value for TopLevelFolderFilter. If you don't do this, Kotor will import all tasks in all folders. Many of there are Microsoft Windows ones, but there may be third party ones as well. You usually don't worry about these, as they are set up by the OS or other applications.
The following example imports tasks that are located in the three top level folders: Backup, Cleanup and Clients:
<add key="TopLevelFolderFilter" value="Backup|Cleanup|Clients" />
ImportRootTasks can be 'true' or 'false' and by default is 'false', meaning tasks in the root are not imported. You can change this to 'true' to also import tasks in the root of the tasks library.
This is the main Kotor application, you can import, edit and create tasks.
You can import tasks by clicking the 'Import' button, this does the same as the command line tool and it supports the same two settings in the configuration file.
You can edit certain properties of existing tasks in the database.
To create new tasks, you have to set the user who should execute them and her password
. The 'Sync' button creates all tasks that are marked in the database, the 'Sync What-if' shows what the 'Sync' button would do without actually doing it.
There is one big command 'Sync' which applies changes you've made to the database to the tasks on the OS.
It only looks at tasks that have an operation other than 'none'. In the database the operation is stored in the Crud column and the supported values are:
None = 0, Create = 1, Delete = 8
By changing the operation you are telling Kotor to perform an action.
After Kotor has performed an 'operation' on a database task, the operation value is set back to zero.
Adding tasks from another machineFirst you have to import the tasks into the database on the other machine, then mark all the ones you want to create with 'Add' in the operation column and run Sync. Kotor will create the tasks on the current machine. Now 'import' the local tasks into the database.
Deleting many tasks Set the operation of the tasks you want to delete to 'Delete' and Sync. To delete them from the database just click 'Import', because 'Import' deletes all existing records and then imports the existing tasks, the ones you have just deleted are now gone.
Renaming a task. This is not supported by the API because the name is used like 'a primary key'.
So to do this, set the operation to 'delete' for the task you want to rename, then Sync. The task is deleted in the task scheduler but not in the database. Now set the operation to 'Add' and change the name to the new desired name and Sync again. You have lost the history of the old task, but everything else is fine.
Moving tasks into a different folder This is similar to renaming. You first set a bunch of files
to 'Delete' and then 'Sync' to delete them. Now set the operations to 'New' and change the 'Path' column. Sync again.
The path column has to start with a backslash '\' and should not have an backslash at the end (except for the root).
Again the history of the old tasks will be gone.
Copy a task Just rename an existing task and mark it as 'Add', then click 'Sync'. There is now a new tasks with the given name. Change the settings in the task scheduler UI and then 'Import Tasks' in Kotor to update the database.
I had to move many tasks from an existing 2008 live server to a new 2008 R2 Server Core server.
I first copied the Kotor files to the old server (no installation required) and ran it as an admin to import the existing tasks into the database. Then I copied the kotor.sdf file down to a local Windows 7 machine.I pointed my local Kotor to that database in kotorgui.exe.config:
<add name="Kotor" connectionString="Data Source=C:\liveserverbackup\kotor.sdf;"/>
I then started Kotor GUI to change the Path for all the tasks to something like 'liveservermove'. I also set the operation on all of them to 'Add'. Finally I fixed some of the names as well. By syncing them, there were now created on my workstation.
I needed to adjust some program locations and schedules, I used the Windows Task Scheduler UI for this. To import just the tasks in question back into my database I changed the TopLevelFolderFilter setting in the config file:
<add key="TopLevelFolderFilter" value="liveservermove"/>
I also copied a new empty database file from the Kotor zip file and pointed my connection string to it. I wanted a fresh empty database without the entries from the old live server.
I opened KotorGui.exe and clicked 'Import Tasks' to get the changed tasks back. Now is the last chance to set the names and the paths for the tasks and then marked them all as 'add'.
Finally I copied the Kotor files including our database file with the prepared tasks to the Server Core machine. Here I could not use the Winforms app, but had to use kotorcli.exe:
kotorcli.exe -s -a username -p apasswordto create all the tasks on the new server.
If all the servers had access to a SQL-Server, I could have used it instead of copying the sdf file around.
Also if you have many tasks, you may want to edit them in bulk. The SQL Server Compact Toolbox project at CodePlex has a standalone (no install required) tool that allows you to use SQL commands or a GUI to update data in an sdf file. Make sure you get the standalone download and not the Visual Studio AddOn.
There are two options of how to store the data about the tasks:
Using a proper SQL-Server, either local or remote, either Express or full. This would give you a central repository for all your task information and you can use familiar tools to do bulk edits on the data.
I recommend this if you have tasks on multiple machine and you already have an instance of Sql-Server running.
2. SQL Server Compact 4.0
If you just have one or two machines and do not have a SQL-Server, you can use this second option. The data is stored in a single file which you can move around and can easily transfer between different machines using xCopy.
SQL Server Compact 4.0 does not require any installation on your machine, so it is a risk-free option.
System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.
The most likely explanation is that you don't have the SQL Server Compact 4 runtime installed, download the run time files and place them in the same directory as the other application files.
Initial public version.