In the previous article we looked at how to export scheduled tasks using PowerShell and Microsoft Management Console. In it, I exported a task on my Windows 8 machine that creates a weekly report using SystemInfo.exe. I exported the task to an XML file and stored it on a file share. Naturally, the next step is to import the scheduled task using Management Console, SchTasks.exe, and PowerShell.
You can manually import a task into any system running the “new” task scheduler (i.e. Vista and later).
On my Windows 2008 R2 server, I’ll navigate to the task scheduler folder where I want to import the task. From here, I can either right-click and select Import Task from the context menu or use the Import Task action. I’ve highlighted the action below in Figure 1.
The server CHI-FP01 is running PowerShell 3.0 but it is only Windows Server 2008 R2, which means I can’t use PowerShell to import the task. However, I can still use the venerable command line tool SchTasks.exe. The /XML parameter will import the specified XML file.
C:\> schtasks /create /xml "\\chi-fp01\it\netsh dump.xml" /tn "Netsh Dump" /ru globomantics\administrator /rp * Please enter the run as password for globomantics\administrator: ******** SUCCESS: The scheduled task "Netsh Dump" has successfully been created. C:\>
Since my XML definition does not include the credentials for the task, I need to specify them at import time. Because I didn’t specify a path for the imported task, Windows created it in the root of my Task Scheduler.
C:\> schtasks /create /tn "\MyCompany\Netsh Dump" /xml "\\chi-fp01\it\netsh dump.xml" /ru globomantics\administrator /rp *
Because SchTasks.exe allows you to manage scheduled tasks remotely, you could automate the import of a task across multiple machines.
The other technique for importing a scheduled task is using PowerShell. This will require PowerShell 3 running on Windows 8 or Windows Server 2012. If you look through the ScheduledTasks module, you won’t find an Import-ScheduledTask cmdlet. It would seem only natural it be there since there is an Export-ScheduledTask, but no.
Instead, we’ll use the Register-ScheduledTask cmdlet. Now, if you read through the help, you’d see the –XML parameter and think all you need to do is something like this:
PS C:\> Register-ScheduledTask -Xml '\\chi-fp01\it\Weekly System Info Report.xml' -TaskName "Weekly System Info Report"
However this generates an exception.
Register-ScheduledTask : The task XML is malformed.
The value for –XML must be a string of XML data. Register-ScheduledTask doesn’t know how to read the file. Instead try something like this:
PS C:\> Register-ScheduledTask -Xml (get-content '\\chi-fp01\it\Weekly System Info Report.xml' | out-string) -TaskName "Weekly System Info Report" -User globomantics\administrator -Password P@ssw0rd –Force
This particular task doesn’t have embedded credentials, so I have to specify the account. –Force will overwrite any existing tasks with the same name. Since I neglected to specify a path, the task is created in the root. If I wanted, I could use the other scheduled task cmdlets to further modify it.
Finally, since Register-ScheduledTask can be used with remote systems, using the –CimSession parameter it is pretty easy to import the same task across multiple Windows 8 or Windows Server 2012 systems.
As useful as it might be to import scheduled tasks, there are a few things to be aware of. First, while obvious it is amazing how often we miss the obvious, the imported scheduled task won’t work if you are trying to run a task based on some command or application element that doesn’t exist. There is no way to know if the task will work until you try to run it.
The same principal applies to scheduled PowerShell jobs. These are scheduled tasks you’ll find under \Microsoft\Windows\PowerShell\ScheduledJobs\. These tasks rely on a job configuration stored locally under AppData. Exporting and importing a scheduled task will not include this information. My recommendation is to simply run the command to create the scheduled job on all the systems where you wish to have the task.
I encourage you to test the export and import techniques in a test environment. Which technique to use depends on the scope of your management challenge and your comfort level.