You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible需求:按顺序重启Windows服务并设置间隔等待

多台Windows主机按顺序重启服务并设置等待间隔的解决方案

以下是两种实用的实现方案,可满足你按指定顺序启动服务并设置等待时间的需求:

方法1:PowerShell脚本(推荐,原生支持远程操作)

该脚本可批量处理多台远程主机,自带错误处理,适合运维场景:

# 替换为你的目标主机名/IP列表
$targetHosts = @("Host01", "Host02", "Host03")

foreach ($hostName in $targetHosts) {
    Write-Host "=== 开始处理主机: $hostName ==="

    # 1. 启动Polling Agent Service
    try {
        Invoke-Command -ComputerName $hostName -ScriptBlock {
            $service = Get-Service -Name "Polling Agent Service" -ErrorAction Stop
            if ($service.Status -ne 'Running') {
                Start-Service -Name "Polling Agent Service"
                Write-Host "已启动 Polling Agent Service"
            } else {
                Write-Host "Polling Agent Service 已处于运行状态"
            }
        }
    } catch {
        Write-Host "主机 $hostName 上启动 Polling Agent Service 失败: $_" -ForegroundColor Red
        continue # 跳过当前主机后续操作,处理下一台
    }

    # 等待60秒
    Write-Host "等待60秒..."
    Start-Sleep -Seconds 60

    # 2. 启动Memcached
    try {
        Invoke-Command -ComputerName $hostName -ScriptBlock {
            $service = Get-Service -Name "Memcached" -ErrorAction Stop
            if ($service.Status -ne 'Running') {
                Start-Service -Name "Memcached"
                Write-Host "已启动 Memcached"
            } else {
                Write-Host "Memcached 已处于运行状态"
            }
        }
    } catch {
        Write-Host "主机 $hostName 上启动 Memcached 失败: $_" -ForegroundColor Red
        continue
    }

    # 等待30秒
    Write-Host "等待30秒..."
    Start-Sleep -Seconds 30

    # 3. 启动watcher
    try {
        Invoke-Command -ComputerName $hostName -ScriptBlock {
            $service = Get-Service -Name "watcher" -ErrorAction Stop
            if ($service.Status -ne 'Running') {
                Start-Service -Name "watcher"
                Write-Host "已启动 watcher"
            } else {
                Write-Host "watcher 已处于运行状态"
            }
        }
    } catch {
        Write-Host "主机 $hostName 上启动 watcher 失败: $_" -ForegroundColor Red
        continue
    }

    Write-Host "=== 主机 $hostName 处理完成 ==="
}

注意事项:

  • 执行脚本的账户需拥有所有目标主机的管理员权限
  • 目标主机需开启WinRM服务,可通过Enable-PSRemoting -Force在目标主机上开启
  • 确保脚本中的服务名称与Windows服务的实际服务名一致(可通过Get-Service命令查看服务名)

方法2:批处理脚本(依赖PsExec工具)

适合习惯使用批处理的场景,需配合PsExec工具:

@echo off
setlocal enabledelayedexpansion

:: 替换为你的目标主机列表(空格分隔)
set "targetHosts=Host01 Host02 Host03"

for %%h in (%targetHosts%) do (
    echo === 开始处理主机: %%h ===

    :: 1. 启动Polling Agent Service
    psexec \\%%h sc start "Polling Agent Service"
    if errorlevel 1 (
        echo 主机 %%h 上启动 Polling Agent Service 失败
        goto nextHost
    )
    echo 已启动 Polling Agent Service
    echo 等待60秒...
    timeout /t 60 /nobreak >nul

    :: 2. 启动Memcached
    psexec \\%%h sc start "Memcached"
    if errorlevel 1 (
        echo 主机 %%h 上启动 Memcached 失败
        goto nextHost
    )
    echo 已启动 Memcached
    echo 等待30秒...
    timeout /t 30 /nobreak >nul

    :: 3. 启动watcher
    psexec \\%%h sc start "watcher"
    if errorlevel 1 (
        echo 主机 %%h 上启动 watcher 失败
        goto nextHost
    )
    echo 已启动 watcher

    echo === 主机 %%h 处理完成 ===
    :nextHost
)

注意事项:

  • 需下载PsExec工具,将其放置在脚本同目录或系统PATH路径中
  • 执行脚本时需以管理员身份运行
  • 若使用Windows XP系统,timeout命令不可用,可替换为ping -n 61 127.0.0.1 >nul(实现60秒等待)

内容的提问来源于stack exchange,提问作者Anand Iyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:50:34