首页 > 解决方案 > 如何将特定的 WMIobject 驱动程序解析为字符串?

问题描述

这样做的目的是从 win32_PnPSignedDriver 获取显示驱动程序版本。我被困在如何过滤掉它。

我从如何将 Get-WMIObject 查询中的数据解析为字符串?并对其进行了修改。但是输出显示的是驱动程序版本信息的完整列表,但我只想打印出显示驱动程序版本。

想知道是否有使用powershell的方法?我有一个为 powershell 编写的基于代码,它需要显示版本才能继续。

我使用的命令是

get-wmiobject -class win32_PnPSignedDriver | select deviceclass -expand DriverVersion

编辑:

预期的输出应该只是其 intel 图形驱动程序的值,例如:10.18.15.4248 我需要将此值解析为变量并将其与验证脚本中的已知固定值进行比较

标签: powershell

解决方案


通过基于 DeviceClass 过滤 WMI 结果来创建显示驱动程序的集合。从过滤结果中选择描述和驱动程序版本。像这样,

# Get all drivers that have display as deviceclass
$ds = gwmi -class win32_PnPSignedDriver | ? { $_.DeviceClass -eq "DISPLAY" }

# Select description and driver's version
$ds | select description,driverversion

description                     driverversion
-----------                     -------------
NVIDIA Quadro P500              24.21.13.9836
Intel(R) UHD Graphics 620       25.20.100.6472

编辑:要仅获取版本字符串而无需任何额外内容,请使用foreachaka处理结果,%如下所示,

# An empty array for the results
$versions = @()

# Add each version as new array element
$ds | % { $versions += $_.driverversion }

# Print results
$versions
24.21.13.9836
25.20.100.6472

# Access the 1st element
$versions[0]
24.21.13.9836

# See the result type
$versions[0].gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

推荐阅读