您可以使用SetSuspendState
方法上的System.Windows.Forms.Application
类来实现这一点。这SetSuspendState
方法是静态方法。
[MSDN] 设置挂起状态 http://msdn.microsoft.com/en-us/library/system.windows.forms.application.setsuspendstate%28v=vs.110%29.aspx
有三个参数:
- State
[System.Windows.Forms.PowerState]
- Force
[bool]
- 禁用唤醒事件
[bool]
要致电SetSuspendState
method:
# 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);
将其放入更完整的函数中可能看起来像这样:
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;
注意:在我的测试中,-DisableWake
据我所知,选项没有产生任何明显的差异。即使此参数设置为,我仍然能够使用键盘和鼠标唤醒计算机$true
.