.net 从 PowerShell 挂起或休眠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20713782/
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
Suspend or hibernate from PowerShell
提问by Trevor Sullivan
I am interested in using Windows PowerShell to suspend or hibernate a computer. How do you achieve this?
我对使用 Windows PowerShell 挂起或休眠计算机很感兴趣。你如何做到这一点?
I am already aware of the Stop-Computerand Restart-Computercmdlets, which are included out of the box, but these do not achieve the functionality that I am after.
我已经知道Stop-Computer和Restart-Computercmdlet,它们是开箱即用的,但是它们没有实现我所追求的功能。
回答by Trevor Sullivan
You can use the SetSuspendStatemethod on the System.Windows.Forms.Applicationclass to achieve this. The SetSuspendStatemethod is a static method.
您可以使用类SetSuspendState上的方法System.Windows.Forms.Application来实现这一点。该SetSuspendState方法是静态方法。
There are three parameters:
共有三个参数:
- State
[System.Windows.Forms.PowerState] - Force
[bool] - disableWakeEvent
[bool]
- 状态
[System.Windows.Forms.PowerState] - 力量
[bool] - 禁用唤醒事件
[bool]
To call the SetSuspendStatemethod:
要调用该SetSuspendState方法:
# 1. Define the power state you wish to set, from the
# System.Windows.Forms.PowerState enumeration.
$PowerState = [System.Windows.Forms.PowerState]::Suspend;
# 2. Choose whether or not to force the power state
$Force = $false;
# 3. Choose whether or not to disable wake capabilities
$DisableWake = $false;
# Set the power state
[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
Putting this into a more complete function might look something like this:
把它放到一个更完整的函数中可能看起来像这样:
function Set-PowerState {
[CmdletBinding()]
param (
[System.Windows.Forms.PowerState] $PowerState = [System.Windows.Forms.PowerState]::Suspend
, [switch] $DisableWake
, [switch] $Force
)
begin {
Write-Verbose -Message 'Executing Begin block';
if (!$DisableWake) { $DisableWake = $false; };
if (!$Force) { $Force = $false; };
Write-Verbose -Message ('Force is: {0}' -f $Force);
Write-Verbose -Message ('DisableWake is: {0}' -f $DisableWake);
}
process {
Write-Verbose -Message 'Executing Process block';
try {
$Result = [System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
}
catch {
Write-Error -Exception $_;
}
}
end {
Write-Verbose -Message 'Executing End block';
}
}
# Call the function
Set-PowerState -PowerState Hibernate -DisableWake -Force;
Note: In my testing, the -DisableWakeoption did not make any distinguishable difference that I am aware of. I was still capable of using the keyboard and mouse to wake the computer, even when this parameter was set to $true.
注意:在我的测试中,该-DisableWake选项没有产生我所知道的任何可区分的差异。我仍然能够使用键盘和鼠标来唤醒计算机,即使将此参数设置为$true.
回答by Rithvik Vibhu
Hope you find these useful.
希望你觉得这些有用。
Shutdown %windir%\System32\shutdown.exe -s
关掉 %windir%\System32\shutdown.exe -s
Reboot %windir%\System32\shutdown.exe -r
重启 %windir%\System32\shutdown.exe -r
Logoff %windir%\System32\shutdown.exe -l
注销 %windir%\System32\shutdown.exe -l
Standby %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Standby
支持 %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Standby
Hibernate %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Hibernate
休眠 %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Hibernate
EDIT:
As pointed out in comment by @mica, the suspend (sleep) actually hibernates. Apparently this happens in windows 8 and above. To 'sleep', disable hibernation OR get an external Microsoft tool (not built-in)
"One of Microsoft's Sysinternals tool is PsShutdown using the command psshutdown -d -t 0it will correctly sleep, not hibernate, a computer"
Source: https://superuser.com/questions/42124/how-can-i-put-the-computer-to-sleep-from-command-prompt-run-menu
编辑:正如@mica 在评论中指出的那样,挂起(睡眠)实际上是在休眠。显然,这发生在 Windows 8 及更高版本中。要“睡眠”,请禁用休眠或获取外部 Microsoft 工具(非内置)“Microsoft 的 Sysinternals 工具之一是 PsShutdown,使用psshutdown -d -t 0它可以正确睡眠而不是休眠计算机的命令”来源:https: //superuser.com /questions/42124/how-can-i-put-the-computer-to-sleep-from-command-prompt-run-menu
回答by tommymaynard
I use the shutdown executable in C:\Windows\System32
我使用 C:\Windows\System32 中的关机可执行文件
shutdown.exe /h
回答by CircaLucid
I tried reducing this down to a one-liner but was getting an error. Here's my solution:
我尝试将其减少为单行,但出现错误。这是我的解决方案:
[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::SetSuspendState("Hibernate", $false, $false);

