vbscript fails when run as scheduled task and user is logged off

Posted on 2004-11-29
Last Modified: 2009-08-16
Can a scheduled task in vbs have a desktop from which windows apps can get "sendkey" input?
Is there an alternative language, rather than vbs, that will work?

When logged on, these scripts run correctly, even when scheduled.  But when the user logs off the scheduled tasks fail (i.e. the edit to the file notefile.txt is not done) because the "sendkey" and the "appactivate" commands fail.

from the administrator desktop, run "note.bat" which uses cscript to call "note.vbs".  It creates logfile.txt and notefile.txt.
When executed, note.vbs attempts to edit notefile.txt.  It works correctly when interactive, fails when scheduled.  Logfile
shows that the return value of appactivate is false when scheduled.    

note:  there have been a number of similar questions recently but the answers have not been clear.  
If it can't be done I would like to hear why!

------------------------------- note.bat  

cd "c:\documents and settings\administrator\desktop"
echo time and date will be written above by vbs script > notefile.txt
cscript note.vbs notefile.txt > logfile.txt

----------------------------- note.vbs

set shell = wscript.createobject("")
shell.currentdirectory =  "c:\documents and settings\administrator\desktop"
mytime = time & " " & date & vbcrlf
wscript.echo "vbs script start " & mytime & vbcrlf
wscript.sleep 1000
return = ("notepad.exe notefile.txt")
wscript.echo "return = " & return
wscript.sleep 1000
return = shell.appactivate ("notepad")
wscript.echo "return = " & return
shell.sendkeys mytime
wscript.sleep 1000
shell.sendkeys "%f"
wscript.sleep 1000
shell.sendkeys "s"
wscript.sleep 1000
shell.sendkeys "%f"
wscript.sleep 1000
shell.sendkeys "x"
wscript.sleep 1000
mytime = time & " " & date & vbcrlf
wscript.echo "vbs script end   " & mytime & vbcrlf

Question by:RROLOFF
    LVL 6

    Accepted Solution

    no ... script language will help... if the user account is not INTERACTIVELY logged on...
    as sendkeys and appactivate work on current INTERACTIVELY logged on user's program windows...

    They will also not work if you just lock the workstation with the right user ...

    it is a SAFETY mechanism... to make sure... only ONE user can login INTERACTIVELY on a machine simultaneously...

    If u can provide a ... original.. script.. then I might be able to help you in modifying it such a way that...
    it doesn't require... INTERACTIVE... sendkeys and appactivate functions...

    other great simulation language is "Auto-IT"....

    LVL 76

    Expert Comment

    by:David Lee
    Sorry to be dense, but I don't understand what you're doing with the script.  That aside, why mess around with Notepad and SendKeys?  Use the FileSystemObject to create a text file and write directly to it.  That will work whether a user is logged in or not.

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFSO.CreateTextFile("c:\documents and settings\administrator\desktop\notefile.txt")
    objTS.WriteLine "Write whatever you want"
    LVL 6

    Expert Comment

    I guess it is just a DUMMY code....


    Author Comment


    Yes, this is a dummy code.  It duplicates the problem I am having with two applications by using a "notepad" dummy code that anyone can run.

    My conclusion is that certain vbs commands, e.g. sendkeys and appactivate, don't run without a user being logged on and having a active display.    I wanted a script that would dial up my ISP (at&t worldnet) at 4am and then download antivirus updates.  My search led me to MSH and VBscript, and I progressed to getting a script that worked interactive, worked when scheduled if I was logged on, but failed when scheduled and logged off.    Microsoft has not clearly documented the fact that some VBscript commands don't work if not logged on.  Shame on Bill.

    I came up with a solution that doesnt' use VBscript.  In Network Connections I make a copy of my dial up connection and give it a 10 minute timeout.  Then I make a shortcut to it and I can schedule a DOS script to run it and dial up at 4am (without using sendkeys to press buttons on the standard at&t gui).  My antivirus software has it's own download scheduler that will try to download signature updates, I set it to run at 4:05 am.  I've solved this problem because my antivirus program has it's own internal scheduler.  I would like to do the same thing with my stock charting program;  it doesn't have a built-in scheduler for downloads.

    It's sort of fun to script a VB activity, put 1 sec delays between button presses, and watch it run.  I would like to better understand why MS can't enable sendkeys and appactivate for a scheduled task.  MS should document this better.

    Thanks for your comments!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Disclosure: Use this tutorial only when no other options helps to get Windows XP running without any problems and you don't want to format the drive. The back up of the data is the responsible of the user, however there is a description of how t…
    It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now