PowerShell query hangs when screensaver runs

Manny
151
Reputation points
Hi everyone
I am creating a PowerShell script to control a program to download prices. If the screensaver doesn't appear then the script works. However, as soon as the screensaver is on then it hangs (lines 12 to 18 do not get run). Lines 7 to 9 run because I see the downloaded file. The folder that stores the CSV files (from lines 15 to 18) is empty. I know its hard to help without having the app in front. Does anyone know why these lines would be impacted by screensaver? Thank you
Script
Start-Process -FilePath "C:\Program Files (x86)\Downloader.exe"
$ProcId = (Get-Process Downloader).id
Sleep -Seconds 60 # give the program 1 minute to open
$wshell = new-object -com wscript.shell
$wshell.AppActivate("Downloader Console")
$wshell.Sendkeys("%d") # press download button
Sleep -Seconds 600 # give the program 10 minutes to finish the download
$wshell.AppActivate("Collection Report")
$wshell.SendKeys("{TAB}{ENTER}") # close the collections window
Sleep -Seconds 20
$wshell.AppActivate("Downloader Console")
$wshell.Sendkeys("^{TAB}^{TAB}") # select convert tab
Sleep -Seconds 20
$wshell.Sendkeys("%c") # press convert button
Sleep -Seconds 600 # give the program 10 minutes to convert the data to CSV
Stop-Process $ProcId
{count} votes
One thing I forgot to mention. This script is going to be part of an SSIS package that is designed to run on a scheduled time so the computer will be on but the screen turn off. The screensaver will be on too. So I need to get the script to work when the screensaver is on. Any help is much appreciated.
Your script is emulating what you would enter on your keyboard. So, how would you switch the focus to the application without dismissing the screensaver first?
Hi @Rich Matheisen
I thought that lines 7, 11, 15 would switch the focus back to the app. It definitely doesn't work. Is there a way to return the focus back to the app?
Replace the appactivate, sendkeys, sleep sequences in your script with loops that activates the application, sends a key that does nothing in your EXE (maybe the F15 key), sleeps for, say two seconds, and repeats until your wait period has elapsed and then move on. That should keep the screensaver from activating.
Also, you're assuming that there's a window with the correct string in its title bar when you use appactivate. Wouldn't it be better to check to see if such a window exists, and wait if it's not there?
Thanks @Rich Matheisen
I may not have been clear on this. My apologies for that. The intent behind the script is to run in SSIS. The desktop will be on 247 so it (and other SSIS jobs) can run at their scheduled times. Currently, the script works if there is no screensaver so one solution is to permanently disable screensaver and just turn the monitor off when not in use. This way there is no screen burn (or whatever they call it .. the residue images that appear from screens being on for a long time). I like the idea of having the screensaver on to protect privacy so the script needs to work when the screensaver is on. Finding ways to prevent the screensaver from coming isn't going to help me. If I misunderstood your proposal please let me know. Is there a way to have the script work when the screensaver is on?
Have you engaged with the vendor's support to find out how to run their application without user intervention?
Hi @Rich Matheisen
I
Are you asking is there is a command line version of the program? If yes, they do not have one.
I would ask them to develop one for you. The inherent problem with trying to automate GUI programs is that scripts can't read the screen. They essentially are sending blind keystrokes and hoping that nothing goes wrong.
Have you run a network trace on the calls that the program makes? If it's just making HTTPS queries you might be able use Invoke-WebRequest and do your own downloads.
Fiddler is also a great tool for tracing HTTP(S) traffic.
Sign in to comment
1 answer
Sort by: Most helpful
Hello Manny,
the main issue will be that during the Sleep (wait) periods the screensaverwill turn on, and then the next SendKeys will be cut (first key sent will be spent to remove the scrensaver).
You can have a $wshell.SendKeys("{NUMLOCK}") to remove first the screensaver after the Sleep(wait) period. The {NUMLOCK} will produce key activity but will not affect anything screen in case the screensaver is not on for any reason.
On the other hand, may be worth considering removing the screensaver using GPO for that or those machines, and replace by "Turn off screen after X inactivity"
Disable ScreenSaver GPO: User Configuration > Administrative Templates > Control Panel > Personalization > Enable Screen Saver (setto Disabled)
Set Turn Off Screen in power plan: Computer Configuration -> Administrative Templates -> System -> Power Management -> Video and Display Settings -> Turn Off the Display (Plugged In) (Set Enabled)
--If the reply is helpful, please Upvote and Accept as answer--
Hi @Limitless Technology
Maybe I did something wrong. The script failed to run the same lines. Below is my script. Is this how you implement your suggestion?
Sign in to comment
Activity