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.
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.
PS C:\> dir C:\users\jeff\appdata\local\Microsoft\Windows\PowerShell\ScheduledJobs Directory: C:\users\jeff\appdata\local\Microsoft\Windows\PowerShell\ScheduledJobs Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 1/18/2013 2:11 PM Demo Scheduled Job
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.
PS C:\> get-scheduledjob "Demo Scheduled Job" | select PS* | format-list PSExecutionPath : powershell.exe PSExecutionArgs : -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft. PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFrom Store('Demo Scheduled Job', 'C:\Users\Jeff\AppData\Local\ Microsoft\Windows\PowerShell\ScheduledJobs');$jobDef.Run()"
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.
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.
Some of the information is stored in nested object properties:
PS C:\> $j = get-scheduledjob Myjob PS C:\> $j.JobTriggers | select * At : 1/15/2013 11:00:00 PM DaysOfWeek : Interval : 1 Frequency : Once RandomDelay : 00:00:00 RepetitionInterval : RepetitionDuration : User : Id : 1 Enabled : True JobDefinition : Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition
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.
PS C:\> import-module Scripts:\ScheduledJobTools
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.
PS C:\> Export-ScheduledJob MyJob -Path c:\work
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.
PS C:\> dir C:\work\myjob.xml Directory: C:\work Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/21/2013 2:59 PM 6522 myjob.xml
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.
PS C:\> Get-ScheduledJob | Export-ScheduledJob -Path G:\Backup
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.
PS C:\> Import-ScheduledJob -Path C:\work\myjob.xml -WhatIf What if: Performing operation "Import-ScheduledJob" on Target "MyJob".
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.
PS Scripts:\> Import-ScheduledJob -Path C:\work\myjob.xml -Passthru Id Name JobTriggers Command Enabled -- ---- ----------- ------- ------- 1 MyJob 1 param ([string]$path) ... True
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.