Powershell解析文本文件 [英] Powershell Parse Text File

查看:140
本文介绍了Powershell解析文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析此文本文件.如果有a沥沥的话,很容易将其解析成一个表格,就像结尾处那样.

I am trying to parse this text file. It would have been easy to parse into a table like at the end, if there was a patter.

我是Powershell的新手,所以不确定如何解决此问题.

And I'm new to Powershell, so not sure how to tackle this problem.

感谢您的帮助.

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
ACPI\PNP0C09\2
USB\ROOT_HUB20\4&361B340A&2
    Driver installed from C:\windows\INF\cmbatt.inf [CmBatt_Inst]. 2 file(s) used by driver:
        C:\windows\system32\DRIVERS\CmBatt.sys
        C:\windows\system32\DRIVERS\battc.sys
ACPI\PNP0C0A\2
    Name: Microsoft ACPI-Compliant Control Method Battery
    Driver installed from C:\windows\INF\cmbatt.inf [CmBatt_Inst]. 2 file(s) used by driver:
        C:\windows\system32\DRIVERS\CmBatt.sys
        C:\windows\system32\DRIVERS\battc.sys

输出

HardwareID      Name            File(s)
----------      ----            -------
USB\ROOT_HUB20...   USB Root Hub        C:\windows\INF\usbport.inf
USB\ROOT_HUB2...    USB Root Hub        C:\windows\system32\drivers\usbhub.sys
USB\ROOT_HUB20..    USB Root Hub        C:\windows\system32\drivers\usbd.sys
ACPI\PNP0C09\1                          C:\windows\INF\machine.inf
ACPI\PNP0C0A\1      Microsoft AC...     C:\windows\INF\cmbatt.inf
                    Microsoft AC...     C:\windows\system32\DRIVERS\CmBatt.sys

推荐答案

RegEx可以使此工作非常简单.导入文件,将其连接在一起,使其成为单个多行字符串,然后将其分割为不以空格开头的行.这样可以得到您的个人记录.然后将它们拆分为换行符,并根据其中的内容解析每一行.再次,RegEx将帮助定义每一行.该代码将为每个部分输出一个对象,该对象具有4个属性,即HardwareID,名称,驱动程序和文件. Files属性是文件数组.

RegEx can make fairly simple work of this. Import the file, join it together so that it's a single multiline string, then split it on lines that don't start with space. That gets you individual records. Then split those on new line characters, and parse each line depending on what's in it. Again, RegEx will help define each line is. This code will output an object for each section, with 4 properties, HardwareID, Name, Driver, and Files. The Files property is an array of files.

(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; 'Driver' = $Driver; 'Files' = $Files}
    }

因此将输出类似的内容

HardwareID                  Name                                            Driver                     Files                                                                          
----------                  ----                                            ------                     -----                                                                          
USB\ROOT_HUB20\4&361B340A&0 USB Root Hub                                    C:\windows\INF\usbport.inf {C:\windows\system32\drivers\usbhub.sys, C:\windows\system32\drivers\usbd.sys} 
ACPI\PNP0C09\1                                                                                         {}                                                                             
USB\ROOT_HUB20\4&361B340A&1 USB Root Hub                                    C:\windows\INF\usbport.inf {C:\windows\system32\drivers\usbhub.sys, C:\windows\system32\drivers\usbd.sys} 
ACPI\PNP0C09\2                                                                                         {}                                                                             
USB\ROOT_HUB20\4&361B340A&2                                                 C:\windows\INF\cmbatt.inf  {C:\windows\system32\DRIVERS\CmBatt.sys, C:\windows\system32\DRIVERS\battc.sys}
ACPI\PNP0C0A\2              Microsoft ACPI-Compliant Control Method Battery C:\windows\INF\cmbatt.inf  {C:\windows\system32\DRIVERS\CmBatt.sys, C:\windows\system32\DRIVERS\battc.sys}

好吧,我假设您并没有真正尝试修改此处的内容,因为通过更改$Driver =$Files +=就像上面的行一样,也将该文件添加到文件数组中.尽管如此,您似乎希望每个文件都包含一行,包括驱动程序文件,因此可能有一个文件数组并非最适合您.您可以做的是为驱动程序文件输出一个对象,然后为每个支持文件输出一个对象,并且在制作对象时可以即时获取每个版本号.因此,您可以使用以下内容替换上面的[PSCustomObject]行:

Ok, I'm going to assume that you didn't really try to modify what I had here since adding the Drivers to the list of other files should be really simple by changing $Driver = to $Files += just like the line above it to add that file to the array of files as well.Although, it looks like you want a line for each file, including the driver file, so maybe having an array for the files isn't best suited for you. What you could do is output an object for the driver file, and then an object for each of the supporting files, and you could get your version number for each on the fly as you make your objects. So you could replace the [PSCustomObject] line above with this:

        [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'}}}

这将输出为:

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)
ACPI\PNP0C09\2                                                                                                                                             
USB\ROOT_HUB20\4&361B340A&2                                                 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 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天全站免登陆