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

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
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,735 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 37,521 Reputation points

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