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

PowerShell批量查询电脑软硬件信息报错求助(含RPC错误)

PowerShell批量查询电脑信息故障排查与修复

问题概述

需求为批量查询多台电脑的系统信息(主机名、型号、登录用户)和硬件信息(显示器数量及详情),流程是读取FayCom.txt中的计算机名,循环查询后导出CSV。当前遇到三个问题:

  • 指定单台电脑时,重复输出信息(次数等于文件内计算机数量)
  • 无法正确获取显示器信息
  • 批量查询触发「RPC服务器不可用」错误

原代码

$computers = Get-Content -Path "\FayCom.txt"
$array1 = @()

foreach ($computer in $computers) 
{
    $query = Get-WmiObject -Class win32_computersystem -Computer $computer
    $name = $query.Name
    $model = $query.Model
    $users = $query.Username
    $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -Computer $computer |findstr "GENUS"
    
    $Object = New-Object PSObject
    $Object | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $name
    $Object | Add-Member -MemberType NoteProperty -Name "Model" -Value $model
    $Object | Add-Member -MemberType NoteProperty -Name "LoggedOnUser" -Value $users
    $Object | Add-Member -MemberType NoteProperty -Name "Monitors" -Value $Monitors
    $array1 += $Object
    }
$array1 | Export-Csv -Path "\test1.txt" -NoTypeInformation

错误信息

Get-WmiObject : The RPC server is unavailable.
At line:3 char:14
+ ...    $query = Get-WmiObject -Class win32_computersystem -Computer $comp ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable.
At line:7 char:17
+ ... $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -Computer  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

故障修复

1. 解决重复输出问题

原因:FayCom.txt中存在重复计算机名或空行,导致循环重复执行同一查询。
修复:读取列表时过滤空行并去重:

$computers = Get-Content -Path "\FayCom.txt" | Where-Object { $_ -match '\S' } | Select-Object -Unique

2. 解决显示器信息无法获取问题

原因:原代码用findstr "GENUS"过滤WMI对象,仅返回无用的元数据,且未解析WmiMonitorID的字节数组属性。
修复:解析显示器的制造商、型号、序列号等属性,转换为可读字符串:

$Monitors = Get-CimInstance -ClassName WmiMonitorID -Namespace root\wmi -ComputerName $computer | ForEach-Object {
    $manufacturer = [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName).Trim([char]0)
    $model = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName).Trim([char]0)
    $serial = [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID).Trim([char]0)
    "$manufacturer - $model (SN: $serial)"
} -Join "; "

3. 解决RPC服务器不可用错误

RPC错误是远程WMI/CIM访问的常见问题,按以下步骤排查:

  • 目标电脑开启**Windows Management Instrumentation (WMI)和Remote Procedure Call (RPC)**服务
  • 目标电脑防火墙允许WMI流量:执行命令netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
  • 确保执行脚本的账号拥有目标电脑的管理员权限
  • 确认目标主机名可正常解析(优先用IP测试排除DNS问题)
  • 改用更现代的Get-CimInstance替代Get-WmiObject(WMI已被淘汰,CIM兼容性更好)

优化后的完整代码

整合所有修复,加入错误处理避免单台故障中断整个循环:

# 读取并处理计算机列表
$computers = Get-Content -Path "\FayCom.txt" | Where-Object { $_ -match '\S' } | Select-Object -Unique
$results = @()

foreach ($computer in $computers) {
    # 初始化结果对象
    $resultObj = [PSCustomObject]@{
        ComputerName   = $computer
        Model          = $null
        LoggedOnUser   = $null
        Monitors       = $null
        Status         = "Success"
        ErrorMessage   = $null
    }

    try {
        # 查询系统信息
        $systemInfo = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -ErrorAction Stop
        $resultObj.Model = $systemInfo.Model
        $resultObj.LoggedOnUser = $systemInfo.UserName

        # 查询显示器信息
        $monitorList = Get-CimInstance -ClassName WmiMonitorID -Namespace root\wmi -ComputerName $computer -ErrorAction Stop | ForEach-Object {
            $manufacturer = [System.Text.Encoding]::ASCII.GetString($_.ManufacturerName).Trim([char]0)
            $model = [System.Text.Encoding]::ASCII.GetString($_.UserFriendlyName).Trim([char]0)
            $serial = [System.Text.Encoding]::ASCII.GetString($_.SerialNumberID).Trim([char]0)
            "$manufacturer - $model (SN: $serial)"
        }
        $resultObj.Monitors = if ($monitorList) { $monitorList -Join "; " } else { "未检测到显示器" }
    }
    catch {
        $resultObj.Status = "Failed"
        $resultObj.ErrorMessage = $_.Exception.Message
    }

    $results += $resultObj
}

# 导出结果到CSV
$results | Export-Csv -Path "\test1.csv" -NoTypeInformation -Encoding UTF8

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 20:50:25