Tre*_*van 16 .net windows powershell
我有兴趣使用Windows PowerShell暂停或休眠计算机.你是如何实现这一目标的?
我已经知道了Stop-Computer
和Restart-Computer
cmdlet一样,它们都是开箱即用的,但是这些并没有实现我所追求的功能.
Tre*_*van 29
您可以SetSuspendState
在System.Windows.Forms.Application
类上使用该方法来实现此目的.该SetSuspendState
方法是一种静态方法.
有三个参数:
[System.Windows.Forms.PowerState]
[bool]
[bool]
要调用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);
Run Code Online (Sandbox Code Playgroud)
将它放入更完整的函数可能看起来像这样:
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;
Run Code Online (Sandbox Code Playgroud)
注意:在我的测试中,该-DisableWake
选项没有产生任何我所知道的可区别的差异.我仍然能够使用键盘和鼠标唤醒计算机,即使此参数设置为$true
.
小智 15
希望你发现这些有用.
关掉 %windir%\System32\shutdown.exe -s
重启 %windir%\System32\shutdown.exe -r
注销 %windir%\System32\shutdown.exe -l
支持 %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Standby
过冬 %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Hibernate
编辑:正如@mica评论中所指出的,暂停(睡眠)实际上是休眠.显然,这发生在Windows 8及更高版本中.要"睡眠",禁用休眠或获取外部微软工具(不是内置的)"微软的Sysinternals工具之一是PsShutdown使用psshutdown -d -t 0
它将正确睡眠的命令,而不是休眠,计算机"来源:https://superuser.com /问题/ 42124 /如何-可以-I-把最计算机到睡眠从命令提示符运行菜单
我在C:\ Windows\System32中使用关闭可执行文件
shutdown.exe /h
Run Code Online (Sandbox Code Playgroud)
小智 6
我尝试将其简化为一行,但出现错误。这是我的解决方案:
[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::SetSuspendState("Hibernate", $false, $false);
Run Code Online (Sandbox Code Playgroud)