0

I have below powershell script which fires refresh command every 1 min or so. It works perfectly fine when run manually from powershell. But I am having issues running it as a service.

while (1) {
  $wsh = New-Object -ComObject WScript.Shell
  $wsh.SendKeys('+{F15}')
  Start-Sleep -seconds 59
}

I used nssm to convert it to a service and when I query it using Get-Service command, I see it as running. However, I no longer see it's effects (system goes to sleep after 1 min as opposed to staying up which happens when I run this manually on powershell.)

Status   Name               DisplayName
------   ----               -----------
Running  ps-refresh         ps-refresh

Are there any extra steps required ?

Naxi
  • 1,504
  • 5
  • 33
  • 72
  • 2
    It will run in the non-interactive session 0 that is reserved for services. There is no desktop it could send Shift+F15 _to_. – CherryDT Jun 01 '22 at 14:48
  • 2
    But that whole thing is a hack anyway. See [this question](https://stackoverflow.com/questions/629240/prevent-windows-from-going-into-sleep-when-my-program-is-running) how to do this properly. If you create a program like that, it should work as service too. – CherryDT Jun 01 '22 at 14:50

0 Answers0