PowerShell 脚本示例,用于监控指定进程,并在其运行时防止系统进入空闲或睡眠状态:
PowerShell 脚本示例,用于监控指定进程,并在其运行时防止系统进入空闲或睡眠状态:
powershellCopy Code
# 定义要监控的进程
$processesToMonitor = @("Minecraft.exe", "Minecraft.exeAddRemove")
# 函数:检查进程是否在运行
function Is-ProcessRunning {
param (
[string]$processName
)
return Get-Process -Name $processName -ErrorAction SilentlyContinue
}
# 主循环
while ($true) {
$isAnyProcessRunning = $false
foreach ($process in $processesToMonitor) {
if (Is-ProcessRunning -processName $process) {
$isAnyProcessRunning = $true
break
}
}
if ($isAnyProcessRunning) {
# 如果检测到进程运行,阻止系统进入空闲状态
# 使用 SendMessage API 防止系统空闲
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SystemInformation]::PowerStatus
[System.Windows.Forms.Application]::DoEvents()
Write-Host "检测到进程运行,已防止系统进入空闲状态。"
}
Start-Sleep -Seconds 10 # 每10秒检查一次
}
使用说明:
-
设置要监控的进程:
- 将
$processesToMonitor中的进程名称替换为你希望监控的进程。
- 将
-
运行脚本:
- 将脚本保存为
.ps1文件。 - 以管理员身份运行 PowerShell,并执行该脚本。
- 将脚本保存为

浙公网安备 33010602011771号