PowerShell查找驱动程序版本 [英] PowerShell Find Driver Version

查看:164
本文介绍了PowerShell查找驱动程序版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用DevCon实用程序,我运行了此命令以获取计算机中安装的所有驱动程序的列表.

Using DevCon utility, I ran this command to get the list of all of the drivers installed in the computer.

devcon.exe driverfiles * > drivers.txt

输出看起来像这样:

USB\ROOT_HUB20\4&361B340A&0
    Name: USB Root Hub
    Driver installed from C:\windows\INF\usbport.inf [ROOTHUB.Dev]. 2 file(s) used by driver:
        C:\windows\system32\drivers\usbhub.sys
        C:\windows\system32\drivers\usbd.sys
ACPI\PNP0C09\1
USB\ROOT_HUB20\4&361B340A&1
    Name: USB Root Hub
    Driver installed from C:\windows\INF\usbport.inf [ROOTHUB.Dev]. 2 file(s) used by driver:
        C:\windows\system32\drivers\usbhub.sys
        C:\windows\system32\drivers\usbd.sys

然后我使用PowerShell脚本来解析文件.感谢TheMadTechnician.

I then used PowerShell script to parse the file. Thanks to TheMadTechnician.

(Get-Content C:\Path\To\File.txt) -join "`r`n" -Split "(?m)^(?=\S)" |
    Where{$_} | 
    ForEach{
        Clear-Variable Files,Driver,Name,HardwareID
        $Files = @()
        $HardwareID = ($_ -split "`r`n")[0].trim()
        Switch -regex ($_ -split "`r`n"){
            "^\s+Name:" {$Name = ($_ -split ':',2)[-1].trim();Continue}
            "^\s+.:\\" {$Files += $_.trim();continue}
            "^\s+Driver" {$Driver = [RegEx]::Matches($_,"(?<=Driver installed from )(.+?)(?= \[)").value;continue}
        }
        [PSCustomObject]@{'HardwareID' = $HardwareID;'Name' = $Name; 'Files' = $Driver; 'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}}
    $Files | ForEach{ [PSCustomObject]@{'HardwareID' = $HardwareID;'Name' = $Name; 'Files' = $_; 'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}Else{'File Not Found'}}}

现在,运行脚本后的输出为:

Now, the output after running the script is:

HardwareID                  Name                                            Files                                  FileVersion                             
----------                  ----                                            -----                                  -----------                             
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                    C:\windows\INF\usbport.inf                                                     
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                    C:\windows\system32\drivers\usbhub.sys 6.3.9600.17238 (winblue_gdr.140723-2018)
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                    C:\windows\system32\drivers\usbd.sys   6.3.9600.17195 (winblue_gdr.140530-1506)
ACPI\PNP0C09\1                                                                                                                                             
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                    C:\windows\INF\usbport.inf                                                     
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                    C:\windows\system32\drivers\usbhub.sys 6.3.9600.17238 (winblue_gdr.140723-2018)
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                    C:\windows\system32\drivers\usbd.sys   6.3.9600.17195 (winblue_gdr.140530-1506)

问题

我想在驱动程序版本中增加一列.

I would like to add an extra column with driver version.

如何找到驱动程序版本并将其列出在名称列旁边?谢谢!

How can I find the driver version and list it next to the name column? Thank you!

编辑

下面的代码从INF文件中解析驱动程序版本. 我没有使用PowerShell的经验,所以如何使用这些信息并将其与上面的代码合并,以添加额外的列和列表驱动程序版本;最好在名称"列旁边.

The code below parses driver version from the INF file. I am not experienced with PowerShell, so how can I used this bit of information and incorporate it with the above code to add an extra column and list driver version; preferably, next to Name column.

$pattern = 'DriverVer\s*=\s*(?:\d+/\d+/\d+,)?(.*)'
Select-String -Pattern $pattern -Path $path |
  select -Expand Matches -First 1 |
  % { $_.Groups[1].Value }

# $path = the INF file

推荐答案

添加新列相对简单.列出这些列是因为它们是数组中对象的属性.因此,现在我们有了这样构建的对象(驱动程序行类似,然后我们将所有剩余文件通过管道传输到该文件中,并添加了换行符以便于阅读):

Adding a new column is relatively simple. The columns are listed because they are properties of the object in the array. So right now we have object that are built like this (the driver line is similar, and then we pipe all remaining files into this, line breaks added for ease of reading):

[PSCustomObject]@{
    'HardwareID' = $HardwareID
    'Name' = $Name
    'Files' = $_
    'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}Else{'File Not Found'}
}

由此,我们得到四个列:"HardwareID",名称",文件"和"FileVersion".如果要添加列,则可以简单地插入另一个属性.在这种情况下,您要在名称"旁边添加一个新列,其中包含每个项目的驱动程序版本.我们可以简单地重新调整当前为每个项目输出驱动程序文件路径(缺少版本号)的行:

From that we get the four columns 'HardwareID, 'Name', 'Files', and 'FileVersion'. If you want to add a column you can simply insert another property. In this case you want to add a new column next to Name that has the version of the driver for each item. We can simply re-purpose the line that currently outputs the driver file path (with missing version number) for each item:

[PSCustomObject]@{'HardwareID' = $HardwareID;'Name' = $Name; 'Files' = $Driver; 'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}}

通过这一行,我们可以删除尝试在"FileVersion"列中列出任何内容的位,并使用提供的代码在其中添加一个新的"DriverVersion"(稍作修改).我们将把$Pattern =行移到脚本的第一行,因为我们不需要为循环的每次迭代都重新分配它.然后,我们将其余代码分配给新的"DriverVersion"属性/列,并将其包装在If($Driver){}语句中,这样对于没有列出驱动程序的条目,它不会引发错误.所以现在看起来像这样:

With that line we can just remove the bit in there that tries to list anything in the 'FileVersion' column, and add a new one in there for 'DriverVersion' using the code that you supplied (with some minor modifications). We're going to move the $Pattern = line to be the first line of the script, since we don't need to re-assign that for each iteration of the loop. Then we are going to assign the rest of your code to the new 'DriverVersion' property/column, and wrap it in a If($Driver){} statement, so that it doesn't throw errors for entries that don't have a driver listed. So that now looks like this:

        [PSCustomObject]@{
            'HardwareID' = $HardwareID
            'Name' = $Name
            'DriverVersion' = If($Driver){Select-String -Pattern $pattern -Path $driver | select -Expand Matches -First 1 |%{ $_.Groups[1].Value }}else{''}
            'Files' = $Driver
            'FileVersion' = '' }

现在,由于我们将该属性添加到了该行,因此我们还需要更新下一行,以便它也包含该属性(尽管具有空白值):

Now, since we added that property to that line, we need to update the next line as well so that it includes that property as well (albeit with a blank value):

        $Files | ForEach{ [PSCustomObject]@{'HardwareID' = $HardwareID;'Name' = $Name; 'DriverVersion'= ''; 'Files' = $_; 'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}Else{'File Not Found'}}}

整个更新后的代码如下:

The updated code as a whole would look something like:

$pattern = 'DriverVer\s*=\s*(?:\d+/\d+/\d+,)?(.*)'
(Get-Content C:\Path\To\File.txt) -join "`r`n" -Split "(?m)^(?=\S)" |
    Where{$_} | 
    ForEach{
        Clear-Variable Files,Driver,Name,HardwareID,DriverVer -ea SilentlyContinue
        $Files = @()
        $HardwareID = ($_ -split "`r`n")[0].trim()
        Switch -regex ($_ -split "`r`n"){
            "^\s+Name:" {$Name = ($_ -split ':',2)[-1].trim();Continue}
            "^\s+.:\\" {$Files += $_.trim();continue}
            "^\s+Driver" {$Driver = [RegEx]::Matches($_,"(?<=Driver installed from )(.+?)(?= \[)").value;continue}
        }

        [PSCustomObject]@{
            'HardwareID' = $HardwareID
            'Name' = $Name
            'DriverVersion' = If($Driver){Select-String -Pattern $pattern -Path $driver | select -Expand Matches -First 1 |%{ $_.Groups[1].Value }}else{''}
            'Files' = $Driver
            'FileVersion' = '' }
        $Files | ForEach{ [PSCustomObject]@{'HardwareID' = $HardwareID;'Name' = $Name; 'DriverVersion'= ''; 'Files' = $_; 'FileVersion' = If(Test-Path $_.Trim()){[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.trim()).FileVersion}Else{'File Not Found'}}}
    }|ft

现在,它输出如下内容:

This now outputs something along the lines of:

HardwareID                  Name                                            DriverVersion  Files                                  FileVersion                             
----------                  ----                                            -------------  -----                                  -----------                             
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                    6.3.9600.17238 C:\windows\INF\usbport.inf                                                     
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                                   C:\windows\system32\drivers\usbhub.sys 6.3.9600.17238 (winblue_gdr.140723-2018)
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                                   C:\windows\system32\drivers\usbd.sys   6.3.9600.17195 (winblue_gdr.140530-1506)
ACPI\PNP0C09\1                                                                                                                                                            
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                    6.3.9600.17238 C:\windows\INF\usbport.inf                                                     
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                                   C:\windows\system32\drivers\usbhub.sys 6.3.9600.17238 (winblue_gdr.140723-2018)
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                                   C:\windows\system32\drivers\usbd.sys   6.3.9600.17195 (winblue_gdr.140530-1506)
ACPI\PNP0C09\2                                                                                                                                                            
USB\ROOT_HUB20\4&361B340A&2                                                 6.3.9600.16384 C:\windows\INF\cmbatt.inf                                                      
USB\ROOT_HUB20\4&361B340A&2                                                                C:\windows\system32\DRIVERS\CmBatt.sys 6.3.9600.16384 (winblue_rtm.130821-1623)
USB\ROOT_HUB20\4&361B340A&2                                                                C:\windows\system32\DRIVERS\battc.sys  6.3.9600.16384 (winblue_rtm.130821-1623)
ACPI\PNP0C0A\2              Microsoft ACPI-Compliant Control Method Battery 6.3.9600.16384 C:\windows\INF\cmbatt.inf                                                      
ACPI\PNP0C0A\2              Microsoft ACPI-Compliant Control Method Battery                C:\windows\system32\DRIVERS\CmBatt.sys 6.3.9600.16384 (winblue_rtm.130821-1623)
ACPI\PNP0C0A\2              Microsoft ACPI-Compliant Control Method Battery                C:\windows\system32\DRIVERS\battc.sys  6.3.9600.16384 (winblue_rtm.130821-1623)

这篇关于PowerShell查找驱动程序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆