PC won't sleep/wake after running script

1

I got fed up with my PC waking from sleep apparently at random intervals and did all the usual stuff, disabling wake timers, task schedules, disallowing LAN and other devices, running powercfg /sleepstudy etc. to no avail.

I found some scripts to disable all wake timers and ran that:

Get-ScheduledTask | ? { $_.Settings.WakeToRun -eq $true -and $_.State -ne "Disabled"} | % { $_.Settings.WakeToRun = $false; Set-ScheduledTask $_ }

Unfortunately, I also ran a script to disallow any device to wake the PC which appears to stall the sleep command until a key press and then shuts it down completely:

(/F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"

This produces several errors.

Although I can understand much of it, I don't have the programming skills to write a script to correct it :-(

Can anybody on here help? (can't restore a system image at the moment - I lent my backup drive+caddy to a buddy who's away)(OK - I'm a Muppett)

EDIT: OK - I ran
PS C:\WINDOWS\system32> @echo off

for /F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (powercfg -devicedisablewake "%%A")
which gave an error of a missing '(' after keyword 'for'. So, I put one in:
PS C:\WINDOWS\system32> @echo off for (/F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (powercfg -devicedisablewake "%%A").
This appears to have worked, although I still don't understand why, because there appears to be one '(' too many.

EDIT 2 Is there a way to reverse it? Can I just replace -devicedisableawake with -deviceenablewake ?

birchyboy

Posted 2018-02-18T19:25:19.620

Reputation: 11

2What are the "several errors" it produces? – Mokubai – 2018-02-18T19:29:29.457

@Mokubai do was unexpected at this time. :) – DavidPostill – 2018-02-18T19:43:19.637

One error was "F : The term '/F' is not recognized as the name of a cmdlet, function, script file, or operable program". Also, the original script had no ( at the start. Windows Powershell Admin said there was a missing ( at the start of the script, I didn't follow that because the ( made it unbalanced. I put one in and the script ran without an error - weird. – birchyboy – 2018-02-19T09:28:59.710

Another error was - The splatting operator '@' cannot be used to reference variables in an expression. '@echo' can be used only as an argument to a command. To reference variables in an expression use '$echo'. – birchyboy – 2018-02-19T09:48:40.430

Answers

0

This produces several errors.

Let's fix it step by step.

> (/F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"
do was unexpected at this time.

The first ( shouldn't be there.

> /F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"
'/F' is not recognized as an internal or external command,
operable program or batch file.

There is a missing for at the start.

From the command line it will only evaluate the first result, so you need to use a batch file.

The %s need to be replaced by %%s in a batch file.

Use the following batch file:

@echo off
for /F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (
  powercfg -devicedisablewake "%%A"
  )

Further Reading

DavidPostill

Posted 2018-02-18T19:25:19.620

Reputation: 118 938