3

我正在尝试在以管理员身份执行的 VBScript 中设置计划任务。如我所见,此脚本设置任务没有问题,但它没有执行,因为它是使用参数创建的run only when user is logged on

代码如下:

Set objShell=Wscript.CreateObject("Wscript.Shell")
command= windir&"\system32\schtasks.exe /create /sc minute /mo "&minutes&" /tn "&APPNAME&" /f /tr ""C:\Windows\System32\wscript.exe '"&getAplicationPath&"\"&wscript.ScriptName&"' cron "
objShell.Run command,0, false

在 msdn.microsoft.com 或在 Google 中搜索时,我都找不到必须设置为禁用该选项的参数。

4

1 回答 1

4

如果您希望任务在没有用户登录的情况下运行,您需要为任务提供运行身份的用户。为此,您需要选项/RU(用于运行方式帐户)和/RP(用于其密码)。如果任务只需要访问本地资源,您可以使用该选项防止密码被存储/NP(不过,您仍然需要在创建任务时提供一次密码)。我认为,后一个选项在 Vista 之前的 Windows 版本上不可用。

引用输出中的相关部分schtasks /create /?

/RU  username      Specifies the "run as" user account (user context)
                   under which the task runs. For the system account,
                   valid values are "", "NT AUTHORITY\SYSTEM"
                   or "SYSTEM".
                   For v2 tasks, "NT AUTHORITY\LOCALSERVICE" and
                   "NT AUTHORITY\NETWORKSERVICE" are also available as well
                   as the well known SIDs for all three.

/RP  [password]    Specifies the password for the "run as" user.
                   To prompt for the password, the value must be either
                   "*" or none. This password is ignored for the
                   system account. Must be combined with either /RU or
                   /XML switch.

/NP                No password is stored.  The task runs non-interactively
                   as the given user.  Only local resources are available.

您的命令行创建可能看起来像这样:

command= "%windir%\system32\schtasks.exe /create" & _
  " /sc minute /mo " & minutes & " /tn " & APPNAME & _
  " /ru " & username & " /rp " & password & _
  " /f /tr ""C:\Windows\System32\wscript.exe '" & _
  getAplicationPath & "\" & wscript.ScriptName & "' cron "
于 2013-11-11T19:54:25.403 回答