先把参考摆上:
参考1: How to: Use schtasks_tool.bat to export and import all Scheduled Tasks on a Windows 2008 server
参考2: schtasks错误: 无法加载列资源
参考3: 用dos命令行导出和导出windows计划任务的详解
参考1中有人说脚本要放在c:\也有人说在win7中放在 c:\windows\system32,试了一下跟放哪儿没关系.
参考2解决了,中文系统下脚本运行不正常的问题 : schtasks 命令需要在 英文代码页环境下运行.
顺便学习了一个切换 cmd 环境 代码页的命令 chcp 437
完整的脚本如下:
chcp 437
rem @echo off
cls
setlocal EnableDelayedExpansion
set runasUsername=name
set runasPassword=pass
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.
使用方法:
1.脚本保存为sch.bat,最好放在英文目录
2.修改脚本中用户名和密码
3.运行 sch.bat export 导出
4.运行 sch.bat import 导入