1. ホーム
  2. スクリプト・コラム
  3. DOS/BAT

dosコマンドでWindowsのスケジュールタスクをインポート/エクスポート

2022-01-25 06:55:36

初心者のためのガイド
会社のソフトウェアのスケジュールタスクをコマンドラインバッチで新システムにインポートするには、すべて情報を調べることで、エクスポートとインポートに使用できるスクリプトを作成します。
エクスポートの使用方法は、スクリプト名+スペース+エクスポートです。
インポートの使用法は、スクリプト名+スペース+インポートです。

以下は、その内容とコメントです。

rem The following is the off command print
@echo off

rem the following to clear the screen
cls

rem The following sets all environment variables for this batch to be applied only to the current batch
setlocal EnableDelayedExpansion

rem The following sets the variables
set runasUsername=domain\administrator
set runasPassword=password

rem The following %1 is the first command line parameter that follows this batch execution (in the execution format: script name + export is equivalent to jumping to ":export")
if %1. == export. call :export
if %1. == import. call :import

rem The following exits the current script, not the entire batch
exit /b 0

:export
rem The following creates a tasks folder, and all errors are not displayed
md tasks 2>nul

rem The following lists all scheduled tasks and specifies the csv format, then finds the lines that do not match the string TaskName and outputs them to a text file
schtasks /query /fo csv | findstr /V /c:"TaskName" > tnlist.txt

rem Loop through each item in each line separated by (,), specify %%i as the first item, specify %%j and %%k as the second item with tokens=1,2 (also possible)
rem specify %%t as the first item, then tokens=1,2 specify the second item %%u and the third item %%v, and the fourth item %%W)

for /F "delims=, tokens=1,2*" %%i in (tnlist.txt) do (
 echo %%i
 echo %%j
 echo %%k
 set tn=%%i
rem The following is to set fn to replace \ with # in the contents of tn
 set fn=!tn:\=#!
 echo !tn!
 echo !fn!
rem The following lists all scheduled tasks and specifies the xml format, which is output to the XXX.xml file in the tasks folder of the current directory
 schtasks /query /xml /TN !tn! > tasks\!fn!.xml
)

rem Remove all scheduled tasks that come with windows
del tasks\#Microsoft*.xml
exit /b 0

rem The following is the import tag, in the execution format: script name+import will jump directly to here to start execution

:import
rem Loop through this directory to find all xml's

for %%f in (tasks\*.xml) do (
  rem Use the call command to call the (:importfile) tag, giving the name found above as an argument
  call :importfile "%%f"
)
exit /b 0

:importfile
rem The following is the first argument given when setting filename=call above, or the second argument if set filename=%2     
set filename=%1

rem The following is set to replace the # character in filename with an empty one
set taskname=%filename:#=%

rem The following set replaces the tasks\ character in filename with an empty one
set taskname=%taskname:tasks\=%

rem The following set replaces the .xml character in filename with an empty one
set taskname=%taskname:.xml=%

rem The following is to create a schedule with the name XXX using the username and password, with the xml file as the parameter
schtasks /create /ru %runasUsername% /rp %runasPassword% /tn %taskname% /xml %filename% 
echo on

終了
これにより、システムに付属していないすべてのスケジュールタスクをエクスポートし、既存のスケジュールタスクをxmlフォーマットでインポートすることができます