Pregunta

I'm trying to set a scheduled task within a VBScript executed as Administrator. This script sets the task without problems as I can see, but it doesn't execute because it's created with the parameter run only when user is logged on.

The code is the following:

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

I couldn't find the parameter that I must set to disable that option neither at msdn.microsoft.com or searching in Google.

¿Fue útil?

Solución

If you want the task to run without a user being logged in you need to provide a user for the task to run as. For that you need the options /RU (for the run-as account) and /RP (for its password). If the task only needs access to local ressources you can prevent the password from being stored by using the option /NP (you still need to provide the password once upon task creation, though). The latter option is not available on Windows versions prior to Vista, I think.

Quoting the relevant sections from the output of 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.

Your commandline creation should probably look somewhat like this:

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 "
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top