将 pnputil 输出解析为特定类的已发布名称 [英] Parse pnputil output to published name for specific class
问题描述
在 PowerShell 中,在我输入pnputil.exe -e"后,我会得到一个驱动程序信息列表,如下所示.我需要从列表中获取班级监视器的发布名称并将oem8.inf"存储到 $monitor_name
中.我可以使用哪个命令来搜索 class = monitor
并将其发布的名称提取到变量中.因为属性发布名称"有空格,所以我不知道如何使这个工作.
In PowerShell, after I type "pnputil.exe -e", I get a list of driver info as below. I need to get class monitor's published name from the list and store "oem8.inf" into $monitor_name
. Which command can I use to search for class = monitor
and extract it's published name to a variable. Because the attribute "published name" has space, so I have no idea how to make this work.
Published name : oem8.inf
Driver package provider : HP
Class : Monitors
Driver date and version : 07/25/2013 2.2.0.0
Signer name : Microsoft Windows Hardware Compatibility Publisher
Published name : oem6.inf
Driver package provider : Canon
Class : Printers
Driver date and version : 06/21/2006 6.1.7600.16385
Signer name : Microsoft Windows
Published name : oem1.inf
Driver package provider : Microsoft
Class : Printers
Driver date and version : 06/21/2006 10.0.14393.0
Signer name : Microsoft Windows
推荐答案
ConnorLSW 的有用回答就是方法去,他的方法说明了一个重要的观点:
ConnorLSW's helpful answer is the way to go and his approach illustrates an important point:
如果您使用 PowerShell cmdlet,则不需要字符串操作来提取信息 - PowerShell 的面向对象特性允许与传统的输出文本解析技术相比,这些解决方案更强大、更方便.
If you use PowerShell cmdlets, there is no need for string manipulation in order to extract information - PowerShell's object-oriented nature allows for solutions that are both more robust and more convenient compared to traditional output-text-parsing techniques.
也就是说,如果需要,PowerShell 也具有强大的字符串解析功能:
That said, if needed, PowerShell has powerful string parsing features too:
$monitor_name = pnputil.exe -e | Select-String -Context 2 'Class :\s+ Monitors' |
ForEach-Object { ($_.Context.PreContext[0] -split ' : +')[1] }
使用您的样本输入,这会产生:
With your sample input, this yields:
oem8.inf
这篇关于将 pnputil 输出解析为特定类的已发布名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!