解析 *.inf 并输出 table

Parsing *.inf and output table

请帮我解决非标准任务(Powershell)。 需要读取 *.inf 文件:C:\Windows\System32\DriverStore\FileRepository" 并带到 table: Manufacter, DeviceID, VerDRV.

ls "C:\Windows\System32\DriverStore\FileRepository" -Recurse -File -Filter *.inf | %{

$mtch = $_ |Select-String -AllMatches -Pattern "^\[(.+)\]$|^([^;].+)=(.+[^;])"
$infObj = New-Object pscustomobject
$mtch.Matches | % {
if ($_.Groups[1].Success) {
    $section = $_.Groups[1].Value
    Add-Member -InputObject $infObj -MemberType NoteProperty -Name $section -Value (New-Object pscustomobject)
} else {
    Add-Member -InputObject $infObj.$section -MemberType NoteProperty -Name $_.Groups[2].Value -Value $_.Groups[3].Value
}
}



$infObj.Version |  Format-Table
$infObj = $null
$mtch = $null
$section = $null }

感谢您的帮助。

我编写了一个名为 Invoke-InstallDrivers (Link to Github) 的函数,创建该函数是为了将驱动程序文件的 GUID 与已安装硬件的 GUID 进行比较,如果匹配则安装,如果匹配不是它会跳过的比赛。基本上我对该函数进行了一些修改以提取您要查找的数据,但是,我发现您指定位置的几个 .INF 文件不包含 "manufacturer" 值,因此我添加了一些逻辑来说明是否没有价值。最后,我在测试工作站上的该路径上的任何 inf 文件中都没有看到任何 DeviceID 字段,因此我假设 ClassGUID 就是您所指的。

Function Get-DriverInfo {

       [CmdletBinding()]
        param(

            [Parameter(Mandatory=$true)]
            [String[]] $Source

         )

    $Drivers = Get-ChildItem -Path "$Source\*" -Recurse | Where {$_.Extension -eq ".inf"} | Select -ExpandProperty FullName  

    Foreach ($Driver in $Drivers)
        {
            Write-Output "Processing File: $Driver"
            $GUID = (Get-Content -Path "$Driver" | Select-String "ClassGuid").Line.Split('=')[-1].Split(' ').Split(';')
            $Version = (Get-Content -Path "$Driver" | Select-String "DriverVer").Line.Split('=')[-1].Split(' ').Split(';')

            if ((Get-Content -Path "$Driver" | Select-String "MfgName") -eq $null)

                {
                    $Manufacturer = "No Manufactuer Listed in INF"
                }

            ELSE

                {
                    $Manufacturer = (Get-Content -Path "$Driver" | Select-String "MfgName").Line.Split('=')[-1].Split(' ').Split(';')
                }

            Write-Output "$Manufacturer, $Version, $GUID"
        }

}

您将通过 运行:

使用上述函数
Get-DriverInfo -Source "C:\Windows\System32\DriverStore\FileRepository"

您可能需要根据自己的喜好调整输出格式,但这应该能让您走上解决问题的正确道路。

编辑 我想我会添加输出的样子,第一部分显示带有制造商的 inf 和其他没有制造商的显示(为了便于阅读,缩短了路径):

PS C:\> Get-DriverInfo -Source "C:\Windows\System32\DriverStore\FileRepository"
Processing File: C:\Windows\System32\DriverStore\FileRepository\...\bcbtumsLD.inf
"Broadcom", 09/25/2013,6.5.1.4800, {e0cbf06c-cd8b-4647-bb8a-263b43f0f974} 

Processing File: C:\Windows\System32\DriverStore\FileRepositor\...\bcmhidnossr.inf 
No Manufactuer Listed in INF, 03/28/2013,1.0.0.101, {745a17a0-74d3-11d0-b6fe-00a0c90f57da}

希望对您有所帮助!

感谢大家!特别感谢 Pavel Pakhomov :)

Clear-Host
Get-ChildItem "C:\Windows\System32\DriverStore\FileRepository" -Recurse -File -Filter '*.inf' | ForEach-Object {
    $fileContent = Get-Content $_.FullName
    #write-host ">>>>>" + $_.FullName

    $dirverVer = ($fileContent | Select-String -SimpleMatch 'DriverVer=') -replace '.*=(.*)',''
    #$Provider = ($fileContent | Select-String -SimpleMatch 'Provider=') -replace '.*=(.*)',''
    $Provider = ($fileContent | Select-String '^\s*Provider\s*=.*') -replace '.*=(.*)',''
    if ($Provider.Length -eq 0) {
        $Provider = ""
    }
    elseif($Provider.Length -gt 0 -And $Provider -is [system.array]) {
        if ($Provider.Length -gt 1 -And $Provider[0].Trim(" ").StartsWith("%")) {
            $Provider = $Provider[1];
        } else {
            $Provider = $Provider[0]
        }
    }
    $Provider = $Provider.Trim(' ')

    if ($Provider.StartsWith("%")) {
        $Provider = $Provider.Trim('%')
        #$Manufacter = ($fileContent | Select-String -SimpleMatch "%$Provider2%=") -replace '.*=(.*)',''
        $Manufacter = ($fileContent | Select-String "^$Provider\s*=") -replace '.*=(.*)',''
    }
    else {
        $Manufacter = ""
    }    

    $ClassGUID = ($fileContent | Select-String -SimpleMatch 'ClassGUID=') -replace '.*=(.*)',''

    if ($Manufacter.Length -eq 0) {
        $Manufacter = $Provider
    } elseif ($Manufacter.Length -gt 0 -And $Manufacter -is [system.array]) {
        if ($Manufacter.Length -gt 1 -And $Manufacter[0].Trim(" ").StartsWith("%")) {
            $Manufacter = $Manufacter[1];
        }
        else {
            $Manufacter = $Manufacter[0];
        }
    }
    $Manufacter = $Manufacter.Trim(' ').Trim('"')

    New-Object -TypeName psobject -Property @{
        Name = $_.Name
        Manufacturer = $Manufacter
        GUID = $ClassGUID
        Version = $dirverVer
    }
} | Format-Table -Property Name,Manufacturer,GUID,Version