Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for backupscheduledtasks.bat

(view source code of backupscheduledtasks.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF NOT "%~4"=="" GOTO Syntax
  4. IF NOT "%~1"=="" IF /I NOT "%~1"=="/Backup" IF /I NOT "%~1"=="/Restore" GOTO Syntax
  5. ECHO.%* | FIND "/?" >NUL && GOTO Syntax
  6. SCHTASKS /?  >NUL  2>&1  || GOTO Syntax
  7. IF "%~1"=="" GOTO BackupAll
  8. IF /I "%~1"=="/Backup" (
  9. 	IF "%~2"=="" (
  10. 		GOTO Syntax
  11. 	) ELSE (
  12. 		CALL :Backup "%~2"
  13. 	)
  14. )
  15. IF /I "%~1"=="/Restore" (
  16. 	IF "%~2"=="" (
  17. 		ECHO Please specify an XML file
  18. 		ECHO.
  19. 		GOTO Syntax
  20. 	) ELSE (
  21. 		IF EXIST "%~2" (
  22. 			FOR %%X IN (%2) DO (
  23. 				CALL :Restore "%%~fX" %3
  24. 			)
  25. 		) ELSE (
  26. 			ECHO File^(s^) not found: "%~2"
  27. 			ECHO.
  28. 			GOTO Syntax
  29. 		)
  30. 	)
  31. )
  32. GOTO:EOF
  33.  
  34.  
  35. :Backup
  36. SETLOCAL
  37. SET TaskName=%~1
  38. IF "%TaskName:~0,1%"=="\" SET TaskName=%TaskName:~1%
  39. SCHTASKS /Query /TN "%Taskname%" >NUL 2>&1
  40. IF ERRORLEVEL 1 (
  41. 	ECHO Task not found: "%TaskName%"
  42. 	ECHO.
  43. 	GOTO Syntax
  44. ) ELSE (
  45. 	SCHTASKS /Query /TN "%TaskName%" /XML > "SCHTASKS.%ComputerName%.Backup.%TaskName%.xml"
  46. )
  47. ENDLOCAL
  48. GOTO:EOF
  49.  
  50.  
  51. :BackupAll
  52. SETLOCAL ENABLEDELAYEDEXPANSION
  53. FOR /F "tokens=1*" %%T IN ('SCHTASKS /Query /FO list ^| FINDSTR /R /B /C:"TaskName: " ^| FINDSTR /R /V /C:"\\.*\\."') DO (
  54. 	CALL :Backup "%%~U"
  55. )
  56. ENDLOCAL
  57. GOTO:EOF
  58.  
  59.  
  60. :Restore
  61. SETLOCAL ENABLEDELAYEDEXPANSION
  62. SET CurrentUser=%ComputerName%\%UserName%
  63. SET XMLFile=%~f1
  64. FOR /F "tokens=3* delims=." %%T IN ("%~n1") DO SET TaskName=%%U
  65. IF NOT EXIST "%XMLFile%" (
  66. 	ECHO File not found: "%XMLFile%"
  67. 	ECHO.
  68. 	GOTO Syntax
  69. )
  70. SET Runas=
  71. SET RunasUser=
  72. FOR /F "tokens=3 delims=<>" %%R IN ('TYPE "%XMLFile%" ^| FIND /I /V "<UserId>%CurrentUser%</UserId>" ^| FIND /I /V "<UserId>SYSTEM</UserId>" ^| FIND /I "<UserId>"') DO (
  73. 	SET RunasUser=%%R
  74. )
  75. IF NOT "%RunasUser%"=="" (
  76. 	IF "%~2"=="" (
  77. 		SET /P RunasPassword=Please enter the password for %RunasUser%: 
  78. 		SET Runas=/RU %RunasUser% /RP !RunasPassword!
  79. 		CLS
  80. 	) ELSE (
  81. 		SET Runas=/RU %RunasUser% /RP %2
  82. 	)
  83. )
  84. echo SCHTASKS /Create /TN "%TaskName%" /XML "%XMLFile%" %Runas%
  85. SCHTASKS /Create /TN "%TaskName%" /XML "%XMLFile%" %Runas%
  86. ENDLOCAL
  87. GOTO:EOF
  88.  
  89.  
  90. :Syntax
  91. ECHO.
  92. ECHO BackupScheduledTasks.bat,  Version 1.02
  93. ECHO Backup and restore scheduled tasks in Task Scheduler's root folder
  94. ECHO.
  95. ECHO Usage:  Backup all: BACKUPSCHEDULEDTASKS
  96. ECHO         Backup one: BACKUPSCHEDULEDTASKS /Backup "taskname"
  97. ECHO         Restore:    BACKUPSCHEDULEDTASKS /Restore "xmlfiles" [ password ^| /Q ]
  98. ECHO.
  99. ECHO Where:  "taskname" is the name of the scheduled task to be backed up
  100. ECHO         "xmlfiles" XML file^(s^) ^(wildcards allowed^) created by this batch
  101. ECHO                    file's backup command or exported in Task Scheduler's GUI
  102. ECHO         "password" is the password for the task's "runas" user
  103. ECHO         /Q         restores the task, but without the "runas" user's password
  104. ECHO                    ^(must be set manually afterwards^)
  105. ECHO.
  106. ECHO Notes:  Only tasks in the Task Scheduler's root folder will be backed up.
  107. ECHO         Each scheduled task in the Task Scheduler's root folder is saved as an
  108. ECHO         XML file in the current directory, with the task's name for file name.
  109. ECHO         When restoring a scheduled task configured for a user other than the
  110. ECHO         current user or SYSTEM, and neither /Q is specified nor a password
  111. ECHO         provided, then you will be prompted for the "runas" user's password.
  112. ECHO         Note that this password will NOT be masked while being entered.
  113. ECHO         Tested in Windows 7 only.
  114. ECHO.
  115. ECHO Written by Rob van der Woude
  116. ECHO http://www.robvanderwoude.com
  117.  
  118. IF "%OS%"=="Windows_NT" EXIT /B 1
  119.  

page last modified: 2024-04-16; loaded in 0.0088 seconds