windows 如何以编程方式启用“允许此设备唤醒计算机”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11750071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:45:28  来源:igfitidea点击:

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

windowsconfigurationpower-managementdevice

提问by Harry Johnston

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.

我需要重新打开此选项,但我无法手动完成 - 计算机太多了!所以,我需要能够通过 API 或脚本来做到这一点。

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

(注意:这不是如何以编程方式启用 LAN 唤醒的副本,因为该问题与 BIOS 设置有关,而此问题与操作系统设置有关。)

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

我已经有一个使用批处理脚本的答案,但是非常欢迎替代解决方案,尤其是如果他们使用 API。

回答by Harry Johnston

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

我在The Old New Thing上找到了解决方案。该powercfg命令允许您操作电源设置,特别是您可以使用-deviceenablewake-devicedisablewake打开和关闭“允许此设备唤醒计算机”选项。

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:

总而言之,这是我刚开始使用的批处理脚本来启用 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.

在这种情况下,我选择在名称包含“网络”或“以太网”一词的所有有效设备上启用该选项;当然,在某些情况下,您可能更愿意选择启用哪些设备。