I am trying to schedule a vbs script to run with regular user rights. The script runs fine when logged in as the user, but when I try to run the script from the task scheduler as "Run whether user is logged on or not", it gets stuck on the following line:
Set IE = CreateObject("InternetExplorer.Application")
I have tried running it with "Run with highest privileges" checked and unchecked. I am running the program from task scheduler as:
program/script: "c:\windows\system32\cscript.exe" arguments: "test.vbs" start in: c:\
Here is the full code:
Set fso = WScript.CreateObject("Scripting.Filesystemobject") set tfo = fso.createTextFile("c:\123.txt") tfo.writeline("1") Set IE = CreateObject("InternetExplorer.Application") tfo.writeline("2") tfo.close
output when ran as "Run only when user is logged on":
output when ran as "Run whether user is logged on or not":
additionally, the task will run correctly as "Run whether user is logged on or not" when using an admin account, but I cannot use an admin account as a solution.Answer1:
You need to grant the user the "Log on as a batch job" privilege. This can be done either via GUI:<ul><li>start
gpedit.msc</li> <li>navigate to <em>Computer Configuration → Windows Settings → Security Settings → Local Policies → User Righst Assignment</em></li> <li>double-click "Log on as a batch job" privilege</li> <li>add the user account</li> <li>click "OK" and close
or on the commandline:
ntrights +r SeBatchLogonRight -u domain\username
ntrights.exe is part of the <a href="http://www.microsoft.com/en-us/download/details.aspx?id=17657" rel="nofollow">Windows Server 2003 Resource Kit Tools</a>, but works on Windows 7 too. You don't have to install the whole package. Instead you can use e.g. <a href="http://7-zip.org/" rel="nofollow">7-zip</a> to open/unpack the
rktools.msi inside the
<strong>Edit:</strong> Since you already did that, the issue is probably that the script can't spawn a GUI application, because you don't have an interactive desktop when the user isn't logged on. Try adding some debugging code to your script:
... On Error Resume Next Set IE = CreateObject("InternetExplorer.Application") If Err Then tfo.writeline Err.Number & vbTab & Err.Description On Error Goto 0 ...
A test-run of this code snippet gave me a "permission denied" error. Apparently limited users cannot create an IE instance in a scheduled task.
That said, what are you trying to achieve with the Internet Explorer object? Using an <a href="https://stackoverflow.com/a/14835871/1630171" rel="nofollow">
XMLHttpRequest</a> might be a better approach for background tasks.