Con*_*bil 3 powershell sleep windows-10
我有一个 powershell 进程,它从远程服务器读取记录并将其复制到本地数据库中。运行时,可能会运行 8-12 小时。
如何防止计算机在此期间关闭(或进入睡眠/休眠模式)?我知道我可以调整“电源和睡眠设置”以将计算机设置为从不睡眠,但这不是我想要的 - 我确实希望它在进程不睡眠时进入睡眠状态。
我知道如果 netflix 或 youtube 视频正在运行,睡眠/休眠会被暂停,我希望计算机在 powershell 进程运行时执行相同的操作。
powershell 进程在桌面上的命令窗口中运行 - 我很高兴屏幕保护程序能够激活,但我不希望发生的是我在 8 小时后唤醒机器并发现该进程只运行了电脑进入睡眠状态前10分钟!
通过一些额外的努力,您可以通过标准powercfg.exe实用程序实现所需的行为,方法是使用按需创建并在脚本运行期间临时激活的自定义始终开启电源方案:
笔记:
寻找# YOUR CODE GOES HERE下面的评论。
对于基于 .NET/Windows API 的替代方案,请参阅此答案。
# Define the properties of a custom power scheme, to be created on demand.
$schemeGuid = 'e03c2dc5-fac9-4f5d-9948-0a2fb9009d67' # randomly created with New-Guid
$schemeName = 'Always on'
$schemeDescr = 'Custom power scheme to keep the system awake indefinitely.'
# Helper function that ensures that the most recent powercfg.exe call succeeded.
function assert-ok { if ($LASTEXITCODE -ne 0) { throw } }
# Determine the currently active power scheme, so it can be restored at the end.
$prevGuid = (powercfg -getactivescheme) -replace '^.+([-0-9a-f]{36}).+$', '$1'
assert-ok
# Temporarily activate a custom always-on power scheme; create it on demand.
try {
# Try to change to the custom scheme.
powercfg -setactive $schemeGuid 2>$null
if ($LASTEXITCODE -ne 0) { # Changing failed -> create the scheme on demand.
# Clone the 'High performance' scheme.
$null = powercfg -duplicatescheme SCHEME_MIN $schemeGuid
assert-ok
# Change its name and description.
$null = powercfg -changename $schemeGuid $schemeName $schemeDescr
# Activate it
$null = powercfg -setactive $schemeGuid
assert-ok
# Change all settings to be always on.
# Note:
# * Remove 'monitor-timeout-ac', 'monitor-timeout-dc' if it's OK
# for the *display* to go to sleep.
# * If you make changes here, you'll have to run powercfg -delete $schemeGuid
# or delete the 'Always on' scheme via the GUI for changes to take effect.
# * On an AC-only machine (desktop, server) the *-ac settings aren't needed.
$settings = 'monitor-timeout-ac', 'monitor-timeout-dc', 'disk-timeout-ac', 'disk-timeout-dc', 'standby-timeout-ac', 'standby-timeout-dc', 'hibernate-timeout-ac', 'hibernate-timeout-dc'
foreach ($setting in $settings) {
powercfg -change $setting 0 # 0 == Never
assert-ok
}
}
# YOUR CODE GOES HERE.
# In this sample, wait for the user to press Enter before exiting.
# Before that, the 'Always on' power scheme should remain in
# effect, and the machine shouldn't go to sleep.
pause
} finally { # Executes even when the script is aborted with Ctrl-C.
# Reactivate the previously active power scheme.
powercfg -setactive $prevGuid
}
Run Code Online (Sandbox Code Playgroud)
您可以从上面创建一个包装器脚本,并将要执行的脚本的路径传递给该脚本。
如果您不介意修改当前活动的方案,您可以使用Kerr 的答案中显示的方法,使用每个设置 powercfg -change <setting> <value-in-minutes>调用(/x/是/-x的别名),在每个调用中使用以下名称之一;通过as代表从不:/change-change<setting>0<value-in-minutes>
monitor-timeout-acmonitor-timeout-dcdisk-timeout-acdisk-timeout-dcstandby-timeout-acstandby-timeout-dchibernate-timeout-achibernate-timeout-dc但请注意,此类更改是持久性的,因此您可能希望稍后恢复原始值,这需要额外的努力。
| 归档时间: |
|
| 查看次数: |
7627 次 |
| 最近记录: |