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

如何结合Get-Content编写PowerShell脚本排查多主机指定进程状态

Check VPDICOMServer Process Status Across Multiple Hosts from a Text File

Got it, let's get your script working with a host list file first—first, let's fix the tiny syntax bug in your original script, then expand it to read from a text file and check remote hosts.

Step 1: Fix the Local Process Check Script

Your original script has a stray closing brace (}) right after the if condition. Here's the corrected local version:

$ProcessName = "VPDICOMServer"
if ((Get-Process $ProcessName -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Process is not running"
} else {
    Write-Host "Process is running"
}

Step 2: Expand to Read from a Host List File

To check multiple hosts from a text file (where each line is a hostname or IP address), we'll use Get-Content to read the file, then loop through each host to check the process. We'll also add error handling for unreachable hosts and make output clearer by including the host name.

This method works without needing full PowerShell Remoting configured in many cases, and handles errors better:

$ProcessName = "VPDICOMServer"
$HostListPath = "C:\path\to\your\hosts.txt" # Replace with your actual file path

# Read all hosts from the text file
$Hosts = Get-Content -Path $HostListPath

foreach ($HostName in $Hosts) {
    Write-Host "`nChecking $HostName..."
    try {
        # Check if the process exists on the remote host
        $Process = Get-CimInstance -ClassName Win32_Process -Filter "Name='$ProcessName'" -ComputerName $HostName -ErrorAction Stop
        
        if ($Process) {
            Write-Host "* $ProcessName is running on $HostName" -ForegroundColor Green
        } else {
            Write-Host "* $ProcessName is NOT running on $HostName" -ForegroundColor Red
        }
    } catch {
        Write-Host "* Failed to check $HostName : $_" -ForegroundColor Yellow
    }
}

Option 2: Using Get-Process (Requires PowerShell Remoting)

If you have PowerShell Remoting enabled on all target hosts, you can use Get-Process with the -ComputerName parameter:

$ProcessName = "VPDICOMServer"
$HostListPath = "C:\path\to\your\hosts.txt"

$Hosts = Get-Content -Path $HostListPath

foreach ($HostName in $Hosts) {
    Write-Host "`nChecking $HostName..."
    $Process = Get-Process -Name $ProcessName -ComputerName $HostName -ErrorAction SilentlyContinue
    
    if (-not $Process) {
        Write-Host "* $ProcessName is NOT running on $HostName" -ForegroundColor Red
    } else {
        Write-Host "* $ProcessName is running on $HostName" -ForegroundColor Green
    }
}

Key Notes

  • Make sure your host list file (hosts.txt) has one hostname/IP per line, no extra spaces.
  • For remote checks, your user account needs appropriate permissions on the target hosts (e.g., admin access to query processes).
  • The try/catch block in Option 1 helps catch issues like unreachable hosts or permission errors, so you don't get silent failures.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:08:00