3

I need to execute a file on a remote machine without logging into the machine. I created scheduled task for the same. PFB the code snippet

scheduled_task = "schtasks /Create /S <> /SC nologon /TN install /TR c:\Temp\msgbox1.vbs /ST " & Hour(now) & ":" & Minute(now)+1 & ":00 /Z" cmdprmpt = "cmd /c " & scheduled_task intReturnCode= oWshShell.Run (cmdprmpt,4,True)

However this code executes only when the user is logged on. Is there a way to execute the same whether the user is logged on or not. -- This can be done manually in Task Scheduler. Can this be automated?

Dipac
  • 31
  • 1
  • 1
  • 3

1 Answers1

4

I think that if you add

/RU <username> /RP <password>

to the call to schtasks.exe you will get "Run whether user is logged on or not" selected. You can also use the /NP instead, which will also give you "Run whether user is logged on or not", but also "Do not store password..." which will limit the accessible resources.

This at least works on the local computer, which is where I normally create tasks from code (part of an installation procedure)

  • I am getting an error when I try in the manner you told. – Dipac Nov 14 '14 at 14:07
  • I just tried it against a remote computer without problems. Which kind of error are you getting? Are you trying from the command line or from the VBscript? If you only try from the VBScript, I would recommend first trying from the command line to make sure you get the right command structure before moving the call into the VBScript. – Lars Lind Nilsson Nov 14 '14 at 14:38
  • I tried this schtasks /Create /S <> /RU <> /RP <> /SC ONCE /TN install /TR <> /ST 16:21:00 /Z – Dipac Nov 14 '14 at 15:20
  • error ERROR: The task XML is missing a required element or attribute. (40,4):EndBoundary: – Dipac Nov 14 '14 at 15:22
  • I got the following error now ERROR: A specified logon session does not exist. It may already have been terminated. Network access: Do not allow storage of passwords and credentials for network authentication- Disabling this option would prevent the error – Dipac Nov 17 '14 at 08:51
  • The ".. XML is missing..." error can can happen if you are using Windows Vista and using the /Z parameter. Either skip the /Z parameter to add the /V1 parameter to force the task to be created in Pre Vista mode. – Lars Lind Nilsson Nov 17 '14 at 10:50
  • The "..specified logon session does not exist..." error can be caused by a security setting on the remote computer not allowing storage of password. I believe that it can be controlled in Administrative Tools -> Local Security Policy -> Local Policies -> Security Options -> "Network access: Do not allow storage of passwords and ...". I belive this should be set to disabled to be able to use the /RU and /RP. – Lars Lind Nilsson Nov 17 '14 at 10:57