gpt4 book ai didi

powershell - 如何在运行 powershell 进程时阻止 Windows 10 机器进入休眠/休眠状态?

转载 作者:行者123 更新时间:2023-12-04 08:28:15 29 4
gpt4 key购买 nike

我有一个 powershell 进程,它从远程服务器读取记录并将它们复制到本地数据库中。运行时,它可能会运行 8-12 小时。

如何防止计算机在此期间关闭(或进入休眠/休眠模式)?我知道我可以调整“电源和 sleep 设置”以将计算机设置为永不休眠,但这不是我想要的 - 我确实希望它在进程没有在运行。

我知道如果 netflix 或 youtube 视频正在运行, sleep /休眠会暂停,我希望计算机在 powershell 进程运行时执行相同的操作。

powershell 进程在桌面上的命令窗口中运行 - 我很高兴屏幕保护程序激活,但我不想发生的是我在 8 小时后唤醒机器并发现该进程只运行了 10 分钟,计算机就进入休眠状态!

最佳答案

通过一些额外的努力,您可以使用标准 powercfg.exe utility 实现所需的行为。 ,通过使用按需创建并在脚本运行期间临时激活的自定义、始终开启的电源方案:

注意:

  • 寻找评论 # YOUR CODE GOES HERE下面。

  • 对于基于 .NET/Windows API 的替代方案,请参阅 this answer .

# 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
}

您可以根据上面的内容创建一个包装器 脚本,并将要执行的脚本路径传递给该脚本。


如果您不介意修改当前方案,您可以使用 Kerr's answer 中所示的方法, 使用每个设置 powercfg -change <setting> <value-in-minutes>调用( /x/-x/change/-change 的别名),使用以下命令之一 <setting>每次通话中的姓名;路过0作为<value-in-minutes>代表从不:

  • monitor-timeout-ac
  • monitor-timeout-dc
  • disk-timeout-ac
  • disk-timeout-dc
  • standby-timeout-ac
  • standby-timeout-dc
  • hibernate-timeout-ac
  • hibernate-timeout-dc

但是请注意,此类更改是持久性的,因此您可能希望稍后恢复原始值,这需要额外的努力。

关于powershell - 如何在运行 powershell 进程时阻止 Windows 10 机器进入休眠/休眠状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65156768/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com