Computer does not stay awake after scheduled task - powershell

I have a scheduled task that wakes up the computer to run a batch file. However the computer turns back off after some time. I have my computer set to (never sleep) so after waking up from the task it should stay on.
However after doing some reading I found out this was because the computer did not wake up from user input.
What I am looking for is a simple script (batch or vb file maybe) I can run via task scheduler that will simulate user input. Maybe hitting the space bar once or moving the mouse.
Running windows 8.1
I tried the following .vbs script without success
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.SendKeys("+{F10}")

You can try this application:
http://mousejiggler.codeplex.com/
It'll simulate mouse movement to keep your computer awake.
If you really want a script try this:
https://gallery.technet.microsoft.com/scriptcenter/Keep-Alive-Simulates-a-key-9b05f980

You can simulate the [F5] key to refresh windows every 2 minutes like this :
Option Explicit
Dim Ws
set Ws = createobject("Wscript.Shell")
Do
Ws.Sendkeys "{F5}"
Call Pause(2)'To sleep for 2 minutes
Loop
'***************************************
Sub Pause(min)
Wscript.Sleep(min*1000*60)
End sub
'***************************************

Related

Display Counter or any other thing till the time any command is running in script in Powershell

I have a script which has some commands that takes time to complete like 30 sec to 1 minute to get completed.
What I am trying to achieve is that till the time command is doing it works in the background we should get some counter or timer or anything getting displayed till the command is running.
When command gets completed, the counter gets stopped.
The user running the script should be aware that yes something is running.
For example like when we open any site and it takes time to load, we get the sand timer or something else as in the display.

AutoHotkey: Could not close the previous instance of this script. Keep waiting?

I've got an AutoHotkey script that presents the following error when I try to run it again:
Could not close the previous instance of this script. Keep waiting?
It's a pretty simple script, with the following settings:
#NoEnv
#SingleInstance force
SendMode Input
DetectHiddenWindows, on
SetWinDelay, -1
I'm launching the script from the command line. I've tried using the /f / /force option and there is no effect.
I want the #SingleInstance Force behaviour described in the docs, which is described as:
Skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.
Turns out the problem was the SetWinDelay instruction.
From the docs:
Although a delay of -1 (no delay at all) is allowed, it is recommended that at least 0 be used, to increase confidence that the script will run correctly even when the CPU is under load.
A delay of 0 internally executes a Sleep(0), which yields the remainder of the script's timeslice to any other process that may need it. If there is none, Sleep(0) will not sleep at all.
When I had it set to -1 the script never had time to process other commands, including whatever exit command was sent to it.
Ensure SetWinDelay is greater than or equal to 0.

autohotkey: how to set up a watchdog

My AHK script's expected run time is somewhere between 5 minutes and 25 minutes depending on the events. And it is mostly doing screen scraping off of a browser, searching for patterns and taking actions (by clicking certain zones on the screen) accordingly. And terminating the browser session when all is done. And I scheduled it to run every hour from windows task scheduler.
My problem is, when there is a network problem or my workstation CPU is having a senior moment, the time allow for the web page to load gets to be insufficient (usually around 15 seconds and it is not feasible to make it longer as contents may change in 30 seconds) Or in the middle of the process something may get hung.
If such a thing happens, I want the AHK script to exit and just before exiting, I want it to terminate all browser sessions (chrome in this particular case)
So far, I am unable to come up with a "single script" solution.
My current state is to start a script which monitors the contents of a file every 30 minutes, and if the contents haven't changed from the last time, open up a new google chrome session and send Shift-Ctrl-Q key sequence to close all instances of chrome. Meanwhile, my main script, updates the same file upon completion with a number different than the previous value in it and it is unique. Deficiency of this approach: I don't know how to kill the other AHK script (hung one) without killing this watchdog script.
And my ultimate desire is to build this functionality, into the main script, which is susceptible to getting hung.
My main script
send #r
send chrome.exe http://myURL{enter}
mousemove 200,200
send ^a
send ^c
;
; using a series of IfInString commands
; determine the condition
; then use mousemove, x, y and mouseclick, l
; commands, do my deed
filedelete, c:\mydir\myfile
fileappend, newnumber, c:\mydir\myfile
exitapp
My watchdog script
loop ; forever
{
; old content is stored in variable PREV
fileread, newval, c:\mydir\myfile
if (newval = PREV)
{
send #r
send chrome.exe
sleep 10000 ; wait for chrome window to pop up
send ^+Q
; I need a way to stop my hung AHK script here but don't know how
}
else
{
PREV = %newval%
sleep 1800000 ; sleep 30 minutes
}
} ; end of loop
f10::exitapp ; when I need to stop watchdog
of course these are not the actual scripts. Just to give you an idea. Otherwise, there are lots of waiting around for pages to load etc. But gist of it is there.
Thank you for your help in advance
I have provided some AutoHotkey code blocks that should resolve the issues you describe.
I would try and write both scripts, so that if both scripts were terminated,
it would be possible to do some checks and resume as if nothing
had happened, i.e. add in more flexibility.
I personally almost never use SetTimer, I just use loops with Sleep,
I don't think it would help improve the script in this case.
I have found that for scripts that have hung, but for where there is no error message to state that they have terminated,
you can retrieve the hWnd (window handle) and PID (process ID) and thus terminate them.
Some of the tasks you describe may be achieved more easily by using UrlDownloadToFile
or WinHttpRequest (see examples on the forum) to download webpages, and then by parsing the html.
Other tasks may be more easily accomplished by using Internet Explorer with COM (Component Object Model),
to query/manipulate web elements in a live webpage.
-
;to launch Chrome and wait for it to open (with an optional delay afterwards)
;if the window is not seen within 60 seconds, ErrorLevel is set to 1 and action can be taken
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://www.google.com/"
WinWaitActive, ahk_class Chrome_WidgetWin_1, , 60
if ErrorLevel
{
MsgBox error ;put code here
}
else
{
Sleep 10000
;put code here
}
;to launch Internet Explorer and wait for it to open (with an optional delay afterwards)
Run, "C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.com/"
WinWaitActive, ahk_class IEFrame
Sleep 10000
;to terminate a script
DetectHiddenWindows, On
vPathScript = %A_ScriptDir%\my script.ahk
WinGet, hWnd, ID, %vPathScript% - AutoHotkey v ahk_class AutoHotkey
IfWinExist, ahk_id %hWnd%
{
WinGet, vPID, PID, ahk_id %hWnd%
Process, Close, %vPID%
}
Process Watchdog using:
- 2 local PC's with shared folders
- 3 ahk Scripts
PC-1 ahk Process script (loops forever)
auto start at windows startup
LOOP
put a WD file on PC-2
run some process
wait some time
go to LOOP
PC-1 ahk WD-1 script
auto start at windows startup (loops forever)
if initial startup set Counter and put WD file in local shared folder
LOOP
wait some time
if local WD file doesn't exist decrement Counter
if Counter = Zero Send Email
if local WD file exists reset Counter and delete local WD file
go to LOOP
PC-2 ahk WD-2 script
auto start at windows startup (loops forever)
if initial startup set Counter and put WD file in local shared folder
LOOP
wait some time
if local WD file doesn't exist decrement Counter
if Counter = Zero Send Email
if local WD file exists reset Counter and delete local WD File
put WD file on PC-1
go to loop
Basic Failure Scenarios:
Process script Fails ... PC-1 and PC-2 WD scripts will send emails
PC-1 Fails ... PC-2 WD script will send email
PC-2 Fails ... PC-1 WD script will send email

Windows Task Scheduler trigger on event, but only once a day

on Windows 7, how do I trigger on first occurance of an event each day to run a small batch file?
I'm trying to kick off a small batch script that runs only for a few seconds when I unlock my PC, but I only want it to run the first time I unlock my PC and never again until the I unlock my PC after 12AM of the next day. I can't tigger on specific time because the time at which I unlock my PC is random. I have been playing with task scheduler for days without success.
You could have the script set a state on first run, which keeps it from running again, for example by exiting immediately instead of doing anything when the state is set.
Then set up a task that triggers on log on to execute said script and another task that runs each day at 12AM to unset/remove the state set by the script. This should give you the effect you want.
A better solution, would be to have the script deactivate (f.e. via schtasks) the task triggered by logging on that called it and have the 12AM task restart the logon triggered task once each day.
I just faced the same problem, so here is my workaround without knowing to much scripting:
I created 4 .bat-files containing basically the following.
start application, afterwards replace bat-file1 with bat-file2
do nothing
replace bat-file1 with bat-file4
start application, afterwards replace bat-file1 with bat-file2
Now I created to scheduled tasks:
The first one runs every day at 12am and runs batch-file 3. Hence, it replaces bat-file1 with bat-file4.
The second one runs after every unlocking of the computer and runs batch-file1.
As you can see in total it does exactly what you want, although it might be a little bit complicated...
On your first unlock it starts your desired script and replaces itself with a dummyfile (the batch file only contains the word exit). After every following unlocking nothing but a hardly noticable cmd popup happens.
At 12am the dummyfile is replaced by the initial batch-file again, to provide your task in the next morning.

Is there a way to run a command line command from JScript (not javascript) in Windows Scripting Host (WSH) cscript.exe?

I'm writing a JScript program which is run in cscript.exe. Is it possible to run a commnad line command from within the script. It would really make the job easy, as I can run certain commands instead of writing more code in jscript to do the same thing.
For example:
In order to wait for a keypress for 10 seconds, I could straight away use the timeout command
timeout /t 10
Implementing this in jscript means more work.
btw, I'm on Vista and WSH v5.7
any ideas? thanx!
You can execute DOS commands using the WshShell.Run method:
var oShell = WScript.CreateObject("WScript.Shell");
oShell.Run("timeout /t 10", 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
If you specifically need to pause the script execution until a key is pressed or a timeout elapsed, you could accomplish this using the WshShell.Popup method (a dialog box with a timeout option):
var oShell = WScript.CreateObject("WScript.Shell");
oShell.Popup("Click OK to continue.", 10);
However, this method displays a message box when running under cscript as well.
Another possible approach is described in this article: How Can I Pause a Script and Then Resume It When a User Presses a Key on the Keyboard? In short, you can use the WScript.StdIn property to read directly from input stream and this way wait for input. However, reading from the input stream doesn't support timeout and only returns upon the ENTER key press (not any key). Anyway, here's an example, just in case:
WScript.Echo("Press the ENTER key to continue...");
while (! WScript.StdIn.AtEndOfLine) {
WScript.StdIn.Read(1);
}
thanx for the help ppl, this was my first post and stackoverflow is awesome!
Also, I figured out another way to do this thing, using the oShell.SendKeys() method.
Here's How:
var oShell = WScript.CreateObject("WScript.Shell");
oShell.SendKeys("cls{enter}timeout /t 10{enter}");
This way you can run almost every dos command without spawning a new process or window
EDIT: Although it seems to solve the problem, this code is not very reliable. See the comments below
Yes, with the WScript.Shell object.
See the docs and samples