?
Solved

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

Posted on 2004-11-29
4
Medium Priority
?
2,924 Views
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("wscript.shell")
shell.currentdirectory =  "c:\documents and settings\administrator\desktop"
mytime = time & " " & date & vbcrlf
wscript.echo "vbs script start " & mytime & vbcrlf
wscript.sleep 1000
return = shell.run ("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
wscript.quit(0)

0
Comment
Question by:RROLOFF
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
kapes earned 1500 total points
ID: 12695377
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".... http://www.hiddensoft.com/autoit3/

0
 
LVL 76

Expert Comment

by:David Lee
ID: 12713444
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"
objTS.Close
0
 
LVL 6

Expert Comment

by:kapes
ID: 12714078
BlueDevilFan,
I guess it is just a DUMMY code....

0
 

Author Comment

by:RROLOFF
ID: 12721053

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!!

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
Step by step guide to Clean and Sort your windows registry! Introduction: Always remember: A Clean registry = Better performance = Save your invaluable time In this article we're going to clear our registry manually! Yes, manually! The e…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 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