以下.vbs创建了所需的任务,没有任何问题(请原谅语法):
Option Explicit
Dim wshell
Set wshell = CreateObject("WScript.Shell")
wshell.Run "schtasks /create /sc once /tn ""Reminder"" /tr ""C:\Windows\System32\cmd.exe \""/c title Alert & mode con: cols=40 lines=10 & color f0 & cls & echo *** Message goes here *** & echo. & echo. & echo. & echo. & echo. & echo. & echo. & echo Press any key to exit & pause > nul\"""" /st 18:00 /sd 08/20/2016 /f"
但是,如果我尝试通过变量传递schtasks
命令字符串,如下所示:
Option explicit
dim strdate, strtime, strmessage, strtask, wshell
strdate = "08/20/2016"
strtime = "18:30"
strmessage = "Turn off pump"
WScript.Echo strdate
WScript.Echo strtime
WScript.Echo strmessage
strtask = """schtasks /create /sc once /tn """"Reminder"""" /tr """"C:\Windows\System32\cmd.exe \""""/c title Alert & mode con: cols=40 lines=10 & color f0 & cls & echo " & strmessage & " & echo. & echo. & echo. & echo. & echo. & echo. & echo. & echo Press any key to exit & pause > nul"""""""" /st " & strtime & " /sd " & strdate & " /f""" & " ,0" & " ,true"
WScript.Echo strtask
Set wshell = CreateObject("WScript.Shell")
wshell.Run strtask
我收到以下错误:
我无法理解为什么我得到这个,毕竟被解析的变量与前一个片段(包括引号)相同......有人可以向我解释一下我缺少什么吗? / p>
修改
感谢Ansgar Wiechers的指导。我已经设法通过删除运行命令选项来获得一个虽然不优雅的脚本,并且本质上检查外部命令错误。见下文:
option explicit
dim strdate, strtime, strmessage, strtask, exitcode, wshell
strdate = "08/21/2016"
strtime = "13:45"
strmessage = "Turn off pump"
WScript.Echo strdate
WScript.Echo strtime
WScript.Echo strmessage
strtask = "schtasks /create /sc once /tn ""Reminder"" /tr ""C:\Windows\System32\cmd.exe \""/c title Alert & mode con: cols=40 lines=10 & color f0 & cls & echo " & strmessage & " & echo. & echo. & echo. & echo. & echo. & echo. & echo. & echo Press any key to exit & pause > nul\"""" /st " & strtime & " /sd " & strdate & " /f"
set wshell = CreateObject("WScript.Shell")
exitcode = wshell.Run(strtask, 0, True)
if exitcode <> 0
then WScript.Echo "External command failed: " & Hex(exitcode)
End If
答案 0 :(得分:2)
我严重怀疑你的原始命令是否有效。 CMD.exe
参数列表周围的嵌套引号应始终使其引发错误。删除那些嵌套引号(只留下整个CMD语句的双引号),并从Run
中删除strTask
方法的参数,任务创建应该按预期工作。
strtask = "schtasks /create /sc once /tn ""Reminder""" & _
" /tr ""C:\Windows\System32\cmd.exe /c title Alert" & _
" & mode con: cols=40 lines=10 & color f0 & cls" & _
" & echo " & strmessage & _
" & echo. & echo. & echo. & echo. & echo. & echo. & echo." & _
" & echo Press any key to exit & pause > nul""" & _
" /sd " & strdate & " /st " & strtime & " /f"
exitcode = wshell.Run(strtask, 0, True)
If exitcode <> 0 Then
WScript.Echo "External command failed: " & Hex(exitcode)
End If
添加一些代码来检查外部命令的退出状态总是一个好主意,这样你就可以检测出错了。
话虽如此,您可以通过一些修改来大大提高此代码的可维护性:
%COMSPEC%
而不是CMD.exe
的文字路径)Function qq(str)
qq = """" & str & """"
End Function
cmdargs = "/c title Alert & mode con: cols=40 lines=10 & color f0 & cls" & _
" & echo " & strmessage & " & echo. & echo. & echo. & echo." & _
" & echo. & echo. & echo. & echo Press any key to exit & pause > nul"
strtask = "schtasks /create /f /sc once /tn Reminder" & _
" /tr " & qq("%COMSPEC% " & cmdargs) & _
" /sd " & strdate & " /st " & strtime
exitcode = wshell.Run(strtask, 0, True)
更好的是,将批处理代码放入实际的批处理文件中:
@echo off
title Alert
mode con: cols=40 lines=10
color f0
cls
echo %~1
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Press any key to exit
pause > nul
并创建任务以使用您的消息运行该批处理文件:
Function qq(str)
qq = """" & Replace(str, """", "\""") & """"
End Function
batchfile = "C:\path\to\your.bat"
strtask = "schtasks /create /f /sc once /tn Reminder" & _
" /tr " & qq(qq(batchfile) & " " & qq(strmessage)) & _
" /sd " & strdate & " /st " & strtime
exitcode = wshell.Run(strtask, 0, True)