PowerShell查询磁盘信息:SD卡PNPDeviceID错误及替代方案咨询
解决方案:无需Win32_DiskDrive获取磁盘Drive Letter、友好名称及PNPDeviceID
针对SD卡场景下Win32_DiskDrive返回通用存储设备ID的问题,可以借助PowerShell的存储模块和PnP设备模块实现更准确的查询,完全不需要依赖WMI的Win32_DiskDrive类,甚至可以全程不使用WMI。
方法1:用Get-Volume + Get-PnpDevice(无WMI依赖)
此方法通过卷信息关联到对应的PnP设备,直接获取SD卡的真实设备ID:
$targetVolumes = Get-Volume | Where-Object { $_.DriveLetter -ne $null -and $_.DriveType -eq 'Removable' -and $_.DriveLetter -ne $env:SystemDrive[0] + ':' } $diskInfo = foreach ($vol in $targetVolumes) { # 通过卷的UniqueId关联到磁盘对象 $disk = Get-Disk | Where-Object { $_.UniqueId -eq $vol.UniqueId } # 通过磁盘的PnpDeviceId获取对应的PnP设备信息 $pnpDevice = Get-PnpDevice -InstanceId $disk.PnpDeviceId -ErrorAction SilentlyContinue [PSCustomObject]@{ DriveLetter = $vol.DriveLetter + ':' FriendlyName = $pnpDevice.FriendlyName PNPDeviceID = $disk.PnpDeviceId } } $diskInfo
方法2:用Get-Disk + Get-PnpDevice(更直接的磁盘关联)
直接从磁盘对象入手,筛选可移动磁盘后关联到盘符和PnP设备:
$removableDisks = Get-Disk | Where-Object { $_.MediaType -eq 'Removable' -and $_.Size -ne $null } $diskInfo = foreach ($disk in $removableDisks) { # 获取磁盘对应的盘符 $driveLetter = $disk | Get-Partition | Get-Volume | Where-Object DriveLetter | Select-Object -ExpandProperty DriveLetter if ($driveLetter -and $driveLetter + ':' -ne $env:SystemDrive) { $pnpDevice = Get-PnpDevice -InstanceId $disk.PnpDeviceId -ErrorAction SilentlyContinue [PSCustomObject]@{ DriveLetter = $driveLetter + ':' FriendlyName = $pnpDevice.FriendlyName PNPDeviceID = $disk.PnpDeviceId } } } $diskInfo
为什么这两种方法更准确?
Get-Disk是Windows存储模块的原生命令,直接与存储堆栈交互,能准确识别SD卡本身的PnP设备ID,而非读卡器的通用设备ID。Get-PnpDevice直接读取系统的PnP设备数据库,返回的友好名称和设备ID都是设备的真实标识,不会出现Win32_DiskDrive的泛化问题。
内容的提问来源于stack exchange,提问作者chemist
相关产品推荐
相关产品推荐

