如果您希望任务在没有用户登录的情况下运行,您需要为任务提供运行身份的用户。为此,您需要选项/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 "