powershell - 如何将特定的 WMIobject 驱动程序解析为字符串?
问题描述
这样做的目的是从 win32_PnPSignedDriver 获取显示驱动程序版本。我被困在如何过滤掉它。
我从如何将 Get-WMIObject 查询中的数据解析为字符串?并对其进行了修改。但是输出显示的是驱动程序版本信息的完整列表,但我只想打印出显示驱动程序版本。
想知道是否有使用powershell的方法?我有一个为 powershell 编写的基于代码,它需要显示版本才能继续。
我使用的命令是
get-wmiobject -class win32_PnPSignedDriver | select deviceclass -expand DriverVersion
编辑:
预期的输出应该只是其 intel 图形驱动程序的值,例如:10.18.15.4248 我需要将此值解析为变量并将其与验证脚本中的已知固定值进行比较
解决方案
通过基于 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
编辑:要仅获取版本字符串而无需任何额外内容,请使用foreach
aka处理结果,%
如下所示,
# 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
推荐阅读
- c# - SignalR - 每组只有两个人的回合制游戏
- python-3.x - API 调用 IBM Watson Natural Language Understanding-xq - Python 或 Postman
- java - StreamCorruptedException:无效的流标头:从文件中读取对象时出现 79737200
- linear-programming - 检查 pyomo 模型和生成的 LP 文件格式是否有效并捕获错误/异常
- javascript - 如何计算一个 php 循环在javascript循环中显示它们?
- excel - 使用格式时避免数字总是四舍五入
- php - PHP未将数据插入MySQL但未显示错误
- java - 通过 MultipartOutput 通过 RestEasy 上传文件
- javascript - 如何在运行 CLI 命令时修复此错误“语法错误”
- c - 写入二进制文件的字符矩阵