Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable "Allow this device to wake the computer" programmatically?

On some computers, the network adapters are, by default, configured with the "allow this device to wake the computer" option turned off. As a result, Wake on LAN won't work.

I need to turn this option back on, but I can't do it by hand - too many computers! So, I need to be able to do it via an API or with a script.

(Note: this is not a duplicate of How to Enable Wake On LAN programmatically because that question is about the BIOS setting whereas this one is about the operating system setting.)

I have an answer already using a batch script, but alternative solutions would be very welcome, especially if they use an API.

like image 483
Harry Johnston Avatar asked Jul 31 '12 22:07

Harry Johnston


1 Answers

I found a solution on The Old New Thing. The powercfg command allows you to manipulate power settings, and in particular you can use the -deviceenablewake and -devicedisablewake to turn on and off the "Allow this device to wake the computer" option.

You can see which devices are capable of doing this with this command:

powercfg -devicequery wake_from_any

You can see which devices have the option currently enabled using:

powercfg -devicequery wake_armed

Putting it all together, this is the batch script I've just started using to enable Wake on LAN:

powercfg -devicequery wake_from_any | findstr /i "network ethernet" >adapters.txt
for /F "tokens=*" %%i in (adapters.txt) do powercfg -deviceenablewake "%%i"
powercfg -devicequery wake_armed | findstr /i "network ethernet" || goto :failed

In this case, I've chosen to enable the option on all valid devices whose name contains the word "network" or the word "ethernet"; in some situations, of course, you might prefer to be more selective about which device(s) you enable.

like image 131
Harry Johnston Avatar answered Oct 04 '22 18:10

Harry Johnston