解析* .inf和输出表

时间:2016-06-12 08:57:54

标签: powershell driver inf

请帮我解决非标准任务(Powershell)。 需要阅读* .inf文件:C:\ Windows \ System32 \ DriverStore \ FileRepository“并带到桌面: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 }

感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

我编写了一个名为Invoke-InstallDrivers(Link to Github)的函数,它是为了将驱动程序文件的GUID与已安装硬件的GUID进行比较而创建的,如果它将安装匹配,如果没有匹配则会跳过。基本上我修改了这个函数来拉动你要查找的数据,但是,我发现你指定位置的几个.INF文件不包含"制造商"值,所以如果没有值,我会添加一些逻辑来说明。最后,我在测试工作站上的那条路径上的任何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}

希望这有帮助!

答案 1 :(得分:0)

感谢大家!特别感谢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 '.*=(.*)','$1'
    #$Provider = ($fileContent | Select-String -SimpleMatch 'Provider=') -replace '.*=(.*)','$1'
    $Provider = ($fileContent | Select-String '^\s*Provider\s*=.*') -replace '.*=(.*)','$1'
    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 '.*=(.*)','$1'
        $Manufacter = ($fileContent | Select-String "^$Provider\s*=") -replace '.*=(.*)','$1'
    }
    else {
        $Manufacter = ""
    }    

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

    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