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

PowerShell远程批量获取磁盘信息脚本无输出问题求助

问题分析与解决方案

核心问题

你的脚本输出为空主要有两个关键原因:

  • 错误的WMI类选择:Win32_LogicalDisk仅能获取逻辑磁盘的基础信息(如盘符、磁盘类型),不包含SSD/HDD的区分属性,而且你只筛选了drivetype字段,就算有结果也只是重复的DriveType=3(本地固定磁盘),无法满足你获取SSD/HDD信息的需求。
  • 潜在的权限/远程连接问题:远程执行WMI需要管理员权限,且目标机器需开启WMI服务、防火墙允许WMI端口(135及动态端口),否则即使Test-Connection能ping通,WMI查询也会静默失败,导致$singleOutput为空。

修正后的脚本

以下脚本改用Win32_DiskDrive获取物理磁盘类型(SSD/HDD),并关联Win32_LogicalDisk匹配对应盘符,同时优化错误排查与输出结构:

$computerList = Get-Content -Path "C:\temp\listapc\pclist.txt"
$output = @()

foreach ($computer in $computerList) {
    if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        Write-Host "$computer 在线,正在查询磁盘信息..."
        try {
            # 获取物理磁盘信息,区分SSD/HDD
            $diskDrives = Get-WmiObject Win32_DiskDrive -ComputerName $computer -ErrorAction Stop | 
                Where-Object { $_.MediaType -match 'Fixed hard disk media' } |
                Select-Object DeviceID, Model, @{Name='IsSSD'; Expression={ $_.MediaType -like '*SSD*' -or $_.InterfaceType -eq 'NVMe' }}
            
            # 关联物理磁盘与逻辑盘符
            foreach ($drive in $diskDrives) {
                $logicalLinks = Get-WmiObject Win32_LogicalDiskToPartition -ComputerName $computer -ErrorAction Stop |
                    Where-Object { $_.Antecedent -like "*$($drive.DeviceID)*" }
                
                $driveLetters = $logicalLinks | ForEach-Object {
                    $driveId = $_.Dependent.Split('=')[-1].Replace('"','')
                    (Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter "DeviceID='$driveId'").DeviceID
                }
                
                $output += [PSCustomObject]@{
                    ComputerName = $computer
                    DiskModel    = $drive.Model.Trim()
                    IsSSD        = $drive.IsSSD
                    DriveLetters = ($driveLetters -join ', ')
                }
            }
        }
        catch {
            Write-Host "$computer 查询失败:$($_.Exception.Message)"
        }
    }
    else {
        Write-Host "$computer 离线,跳过..."
    }
}

$output | Out-File -FilePath "C:\temp\resultdiskinfo.txt" -Encoding UTF8

关键改进点

  • 用Win32_DiskDrive的MediaType(含SSD关键字)和InterfaceType(NVMe接口)判断磁盘是否为SSD,精准匹配需求。
  • 通过Win32_LogicalDiskToPartition关联物理磁盘与逻辑盘符,让结果更直观易懂。
  • 添加try/catch捕获查询错误,便于快速定位权限或服务问题。
  • 指定UTF8编码输出,避免中文环境下的乱码问题。

注意事项

  1. 必须以管理员身份启动PowerShell运行脚本,否则远程WMI查询会因权限不足失败。
  2. 目标机器需开启Windows Management Instrumentation服务,可通过命令netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes快速配置防火墙允许WMI访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 08:02:55