Link to home
Start Free TrialLog in
Avatar of Valimai
Valimai

asked on

Win2008 command line export / import scheduled tasks

Hello,

I would like to know how to export tasks and import tasks using command line in windows 2008.

Currently i have this command which exports all tasks to a single xml file.
schtasks /query /xml > AllTasks.xml

However, when trying to import using the command
schtasks /create /xml > AllTasks.xml
... it appears to only want to accept a single task of xml, where AllTasks.xml is a list of all tasks.

thank you
Avatar of mohammedkasim21
mohammedkasim21

Hello,

I am giving you the link to import and export of Windows Vista - and its very similar in Windows 2008
http://www.toxzen.co.za/tutorials/microsoft/how-to-export-vista-scheduled-tasks.html

Mohammed
Avatar of Valimai

ASKER

I have seen that link. But the same problem remains, I have an xml file with many scheduled task xml documents in it. And I cannot import unless it only contains one task.
I'm afraid you won't find an answer to your question. Windows 2008 requires you to import tasks one by one from an XML file. Even when you open the AllTasks.XML file in notepad, you won't find any XML line for the Task Name (just a remarked line which gets ignored by xml reader), which means that they didn't intend for the end-user to import bulk tasks.

Something you can do is to separate your tasks from the AllTasks XML file into individual XMLs and import them then by giving them individual names. I couldn't even find a third-party software for this so far.
Avatar of Valimai

ASKER

Hi efusion,

RE: May need to separate the xml before importing.

I thought this may be the case. I needed to ask the question before i went off and made a custom .net console app or something.

I believe this is the answer but will wait a coupld of days (weekend) to see if anyone else has a quicker method than a .net app.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Valimai

ASKER

Hi Qlemo,

thank you so very much for the script! Worked very well. I modified it slightly, so if anyone was interested in the version i used, here it is....

cheers
rem @echo off
cls
setlocal EnableDelayedExpansion
 
set runasUsername=myuser
set runasPassword=mypassword
 
 
 
if %1. == export. call :export
if %1. == import. call :import
exit /b 0
 
 
:export
md tasks 2>nul
 
schtasks /query /fo csv | findstr /V /c:"TaskName" > tnlist.txt
 
for /F "delims=," %%T in (tnlist.txt) do (
  set tn=%%T
  set fn=!tn:\=#!
  echo  !tn!
  schtasks /query /xml /TN !tn! > tasks\!fn!.xml
)
 
rem Windows 2008 tasks which should not be imported.
del tasks\#Microsoft*.xml
exit /b 0
 
 
 
:import
for %%f in (tasks\*.xml) do (
	call :importfile "%%f"
)
exit /b 0
 
 
:importfile
  	set filename=%1
 
	rem replace out the # symbol and .xml to derived the task name
	set taskname=%filename:#=%
	set taskname=%taskname:tasks\=%
	set taskname=%taskname:.xml=%
 
	schtasks /create /ru %runasUsername% /rp %runasPassword% /tn %taskname% /xml %filename%
	echo.
	echo.

Open in new window

Thanks for this code - very helpful!