Win10 batch import and export batch scripts of windows scheduled tasks

Batch import and export batch scripts of windows scheduled tasks

2017-11-16  16:19:31 yyusea  Reading number: 2705

First put the reference on:

Reference 1:   How to: Use schtasks_tool.bat to export and import all Scheduled Tasks on a Windows 2008 server
Reference 2:   schtasks error: Cannot load column resources

Reference 3:  Detailed explanation of exporting and exporting windows scheduled tasks with dos command line

 

In reference 1, some people said that the script should be placed in c:\, and some people said that it should be placed in c:\windows\system32 in win7.

Reference 2 solves the problem that the script runs abnormally under the Chinese system: The schtasks command needs to be run in the English code page environment.

By the way, I learned a command to switch the cmd environment code page chcp 437

The complete script is as follows:

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.

 

 

Instructions:

1. Save the script as sch.bat, preferably in the English directory

2. Modify the user name and password in the script

3. Run sch.bat export to export

4. Run sch.bat import to import

Guess you like

Origin blog.csdn.net/xcntime/article/details/89671211