在PowerShell中,输入“pnputil.exe -e”后,我会得到一个驱动程序信息列表,如下所示。我需要从列表中获取类监视器的已发布名称,并将“oem8.inf”存储到$monitor_name
中。我可以使用哪个命令来搜索class = monitor
并将其发布的名称提取到变量中。因为“已发布名称”属性有空格,所以我不知道如何使其工作。
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
答案 0 :(得分:2)
ConnorLSW's helpful answer是要走的路,他的方法说明了一个重点:
如果您使用 PowerShell cmdlet ,则无需字符串操作以提取信息 - PowerShell的对象 - 与传统的输出文本解析技术相比,面向本质的解决方案可以提供更强大,更方便的解决方案。
如果需要,PowerShell还具有强大的字符串解析功能:
$monitor_name = pnputil.exe -e | Select-String -Context 2 'Class :\s+ Monitors' |
ForEach-Object { ($_.Context.PreContext[0] -split ' : +')[1] }
通过输入样本,可以得到:
oem8.inf
答案 1 :(得分:1)
您可以使用WMI通过Get-WMIObject
cmdlet(别名gwmi
)执行此操作:
gwmi Win32_PnPSignedDriver | ? DeviceClass -eq "MONITOR"
将返回一些详细信息,您可以使用以下内容将其缩小到InfName
:
gwmi win32_PnPSignedDriver | ? DeviceClass -eq "MONITOR" | Select InfName
InfName
-------
oem30.inf
oem30.inf
答案 2 :(得分:0)
也标记 mklement0
有用的答案。如果您只需要监视器的所有对象数据,您可以这样做。
Clear-Host
(PNPUtil /Enum-Drivers /class Display |
Select-Object -Skip 2) |
Select-String -Pattern 'Monitors' -Context 3,4
# Results
<#
Published Name: oe...
Original Name: tpl...
Provider Name: L...
> Class Name: Monitors
Class GUID: {4d36e...
Driver Version: 11/...
Signer Name: Microsof...
Published Name: oem...
Original Name: tp...
Provider Name: L....
> Class Name: Monitors
Class GUID: {4d36...
Driver Version: 06/...
Signer Name: Microsof...
#>
根据需要进行挑选,或者,创建并使用自定义对象以相同的方式获取您的属性,然后只需选择即可。例如:
Clear-Host
((PNPUtil /Enum-Drivers /class Display |
Select-Object -Skip 2) |
Select-String -Pattern 'Class Name:' -Context 3,4) |
ForEach {
[PSCustomObject]@{
PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
OriginalName = $PSItem.Context.PreContext[1] -replace '.*:\s+'
ProviderName = $PSItem.Context.PreContext[2] -replace '.*:\s+'
ClassName = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
ClassGUID = $PSItem.Context.PostContext[0] -replace '.*:\s+'
DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
SignerName = $PSItem.Context.PostContext[2] -replace '.*:\s+'
}
}
# Results
<#
PublishedName : oe...
OriginalName : amd...
ProviderName : AMD
ClassName : Sys...s
ClassGUID : {4d...
DriverVersion : 02/...
SignerName : Microsof...
...
PublishedName : oem4...
OriginalName : w...
ProviderName : West...
ClassName : WD D...
ClassGUID : {8496e8....
DriverVersion : 11...
SignerName : Microsoft W...
#>
或者表格格式,或者保存到csv。
Clear-Host
((PNPUtil /Enum-Drivers /class Display |
Select-Object -Skip 2) |
Select-String -Pattern 'Class Name:' -Context 3,4) |
ForEach {
[PSCustomObject]@{
PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
OriginalName = $PSItem.Context.PreContext[1] -replace '.*:\s+'
ProviderName = $PSItem.Context.PreContext[2] -replace '.*:\s+'
ClassName = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
ClassGUID = $PSItem.Context.PostContext[0] -replace '.*:\s+'
DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
SignerName = $PSItem.Context.PostContext[2] -replace '.*:\s+'
}
} |
Format-Table -AutoSize
# Results
<#
PublishedName OriginalName ProviderName ClassName ClassGUID DriverVersion SignerName
------------- ------------ ------------ --------- --------- ------------- ----------
...
Clear-Host
((PNPUtil /Enum-Drivers /class Display |
Select-Object -Skip 2) |
Select-String -Pattern 'Class Name:' -Context 3,4) |
ForEach {
[PSCustomObject]@{
PublishedName = $PSItem.Context.PreContext[0] -replace '.*:\s+'
OriginalName = $PSItem.Context.PreContext[1] -replace '.*:\s+'
ProviderName = $PSItem.Context.PreContext[2] -replace '.*:\s+'
ClassName = ($PSitem | Select-String -Pattern 'Class Name:') -replace '.*:\s+'
ClassGUID = $PSItem.Context.PostContext[0] -replace '.*:\s+'
DriverVersion = $PSItem.Context.PostContext[1] -replace '.*:\s+'
SignerName = $PSItem.Context.PostContext[2] -replace '.*:\s+'
}
} |
Where-Object -Property ClassName -EQ 'Monitors' |
Select-Object -Property PublishedName
# Results
<#
PublishedName
-------------
oem11.inf
oem67.inf
#>