我正在尝试使用批处理文件禁用本地系统上的 Windows 任务计划程序作业,但前提是该作业已在远程系统上启用并运行。显然,“ [servername]
”将被实际的远程服务器名称替换。
它对于任务名称中没有空格的任务计划程序作业来说运行良好。但是,下面的代码片段对于作业名称中包含空格的任务名称(例如(“测试示例作业”))不起作用。
批处理文件
for /f "tokens=1" %%j in ('schtasks /Query /S [servername] /TN "Test Sample Job" /NH ^| findstr "Ready ^| Running"') do schtasks /Change /Disable /TN "%%j"
答案1
如果看起来您需要规划使用此逻辑的作业名称之间的空格数,然后相应tokens
地调整variables
。
为了 ”测试样本作业“具体用法:
for /f "tokens=1-9" %%j in ('schtasks /Query /S [servername] /TN "Test Sample Job" /NH ^| findstr "Ready ^| Running"') do schtasks /Change /Disable /TN "%%j %%k %%l"
笔记: 这对于没有空格的作业名称不起作用,因此您需要为带空格和不带空格的作业名称编写一个脚本。您需要为带有 2 个空格、3 个空格、4 个空格等的作业名称编写一个单独的脚本,因此您可能需要更好地标准化作业命名或相应地规划脚本。
更多资源
FOR /?
tokens=x,y,m-n - specifies which tokens from each line are to be passed to the for body for each iteration. This will cause additional variable names to be allocated. The m-n form is a range, specifying the mth through the nth tokens. If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. Some examples might help: FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse. %i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'. Remember, FOR variables are single-letter, case sensitive, global, and you can't have more than 52 total active at any one time.