Petri

How to Export and Import PowerShell Scheduled Jobs

In previous articles I’ve written about how to export scheduled tasks and import scheduled tasks in PowerShell. In those articles, I made mention about the potential downside to exporting and importing PowerShell scheduled jobs. A scheduled job is a new feature in PowerShell 3.0 that allows you to set up a background job but register it with the task scheduler. Because this is a PowerShell feature, it is available on any computer running PowerShell 3.0.

Sponsored

Bear with me here: In order to understand why exporting and importing is a bit more difficult you need to understand how a scheduled job works. When you create a scheduled job from within PowerShell, the job definition is stored under your AppData folder.

Within the folder is a scheduled job definition XML file. The task scheduler essentially loads the job definition into a PowerShell session and runs it. You can see the actual command by looking at the scheduled job.

To export this job and import it on another machine would mean getting the files and folders in ScheduledJobs and recreate it. Frankly, that is too much work. I also wouldn’t be able to automatically recreate the scheduled task unless I was running Windows 8 or Windows Server 2012. No, I need a solution that can work on older systems running PowerShell 3.0 to export and import scheduled jobs. So I wrote one, which I’m happy to share with you.

Sponsored

Finding Scheduled Job Details

The essence of an export/import tool is to recreate the item as close as possible to the original. In the case of a scheduled job, this means getting all the information required for Register-ScheduledJob. This also means recreating the job trigger and any job options. Using help, it isn’t too difficult to learn what pieces of information I needed to recreate a scheduled job. The tricky part was then finding that information in the current job object.

As you can see in the image below, the job object is rich.

scheduled jobs

Some of the information is stored in nested object properties:

You can see what I’ve done in the final script, so I won’t go through every step of the process. The bottom line is that once I figured out what data needed to be exported, I selected the properties and exported to an XML file using Export-Clixml.

Exporting a Scheduled Job

To export a scheduled job, you will first need to export my ScheduledJobTools module, which you can download at the bottom of this article.

This version of the module only has two functions. Both functions have complete help and examples. To export a scheduled job, all you need is to specify the job name and a destination path.

The tool will not write anything to the pipeline unless you use –Passthru, then it will display the XML file object. The default behavior is create an XML file in the path using the job name as the file name.

If you want to rename the XML file, go right ahead.

This is also a handy tool for backing up all of your scheduled jobs.

Importing a Scheduled Job

On the target machine, or your own if you are restoring a deleted job, again import the module.

Next, run the Import-ScheduledJob function, specifying the path to the XML file.

I included support for –Whatif. The tool will not write anything to the pipeline, unless you use –Passthru, in which case it will write the job object to the pipeline.

 

Remember, that I’m calling Register-ScheduledJob in the function so you need to run this in an elevated session. If your scheduled job happens to use alternate credentials, you will be prompted for the password upon import. My export process only captures the account name in the XML file — not the password. But now my scheduled job has been imported and is ready to run. Of course, I’m assuming that if your command has any dependencies such as external scripts or tools that you have handled copying them separately.

Download the ScheduledJobTools Module

You can download the ScheduledJobTools module from Petri IT Knowledgebase. I suggest extracting the ScheduledJobTools module from the zip file to your Windows PowerShell\Modules folder. Again, I think the best solution is to rerun your original job creation command on the other computers. But if you no longer have that code or simply want to backup of existing schedule job definitions, I think this module will get the job done.

Tagged

Sponsored

Join The Conversation

Sponsors

Sponsors