在win下基于powershell的Qb和mp检测是否在运行

发布于 2025-04-09  24 次阅读


# 定义程序路径和名称
$qbittorrentPaths = @(
    "D:\qbittorrent\qbittorrent-sl\qbittorrent.exe",
    "D:\qbittorrent\qbittorrent-xz\qbittorrent.exe"
    "D:\qbittorrent\qbittorrent-bz\qbittorrent.exe"
)
$moviePilotDirectory = "D:\MoviePilot\MoviePilot\project"
$processName = "MoviePilot"
$serviceProcessName = "Service"
$checkInterval = 30  # 检查间隔(秒)

while ($true) {
    # 检查 MoviePilot.exe 是否正在运行
    $process = Get-Process -Name $processName -ErrorAction SilentlyContinue
    
    if (-not $process) {
        Write-Host "$processName 已关闭!" -ForegroundColor Red
        
        # 强制关闭 Service.exe 和 MoviePilot.exe
        $runningServiceProc = Get-Process -Name $serviceProcessName -ErrorAction SilentlyContinue
        if ($runningServiceProc) {
            Write-Host "正在关闭 $serviceProcessName..."
            Stop-Process -Name $serviceProcessName -Force
        }

        # 切换到 MoviePilot 的文件夹并启动程序
        Write-Host "正在重启 $processName..."
        Set-Location -Path $moviePilotDirectory
        Start-Process -FilePath ".\MoviePilot.exe"
    } else {
        Write-Host "$processName 正在运行。" -ForegroundColor Green
    }
    
    # 检查 qBittorrent
    foreach ($path in $qbittorrentPaths) {
        $isRunning = Get-Process | Where-Object { $_.MainModule.FileName -eq $path } -ErrorAction SilentlyContinue
        
        if ($isRunning) {
            Write-Host "$path 正在运行..."
        } else {
            Write-Host "$path 未运行,正在重启..."
            Start-Process $path
        }
    }

    Start-Sleep -Seconds $checkInterval
}

将上边程序复制下来保存到一个txt文件中
然后重命名为.ps1文件
例如test.txt
重命名为
test.ps1

然后
在win搜索栏搜索powershell运行输入以下代码

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 是一条 PowerShell 命令,用于允许:

  • 本地脚本直接运行。
  • 下载的脚本需要可信签名才能执行。

    在桌面右键单击,选择新建快捷方式
  • powershell.exe -NoProfile -ExecutionPolicy Bypass -File "PS1文件存放路径"
  • 例如
    powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Users\NAS\Desktop\test.ps1"

脚本解释


即需要修改的区域只有



  • # 定义程序路径和名称 $qbittorrentPaths = @( "D:\qbittorrent\qbittorrent-sl\qbittorrent.exe", "D:\qbittorrent\qbittorrent-xz\qbittorrent.exe" "D:\qbittorrent\qbittorrent-bz\qbittorrent.exe" ) $moviePilotDirectory = "D:\MoviePilot\MoviePilot\project"


这几个路径
且 在这个"D:\qbittorrent\qbittorrent-bz\qbittorrent.exe"下边还可添加更多的程序|
例如这样


  • # 定义程序路径和名称 $qbittorrentPaths = @( "D:\qbittorrent\qbittorrent-sl\qbittorrent.exe", "D:\qbittorrent\qbittorrent-xz\qbittorrent.exe" "D:\qbittorrent\qbittorrent-bz\qbittorrent.exe"

"D:\qbittorrent\qbittorrent-bz-1\qbittorrent.exe"

"D:\qbittorrent\qbittorrent-bz-2\qbittorrent.exe"
)


HELLO WORLD