How to extract specific tables from html file using native powershell commands?

后端 未结 2 1538
梦谈多话
梦谈多话 2020-12-10 17:10

I make use of the PAL tool (https://pal.codeplex.com/) to generate HTML reports from perfmon logs within Windows. After PAL processes .blg files from perfmon it dumps the in

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 18:11

    I see you accepted an answer but I thought I'd add a RegEx solution in here too. No COM objects needed for this one, and should be PSv2 friendly I'm pretty sure.

    $Path = 'C:\Path\To\File.html'
    [regex]$regex = "(?s)"
    $tables = $regex.matches((GC C:\Temp\test.txt -raw)).groups.value
    ForEach($String in $tables){
        $table = $string.split("`n")
        $CurTable = @()
        $CurTableName = ([regex]'TABLE ID="([^"]*)"').matches($table[0]).groups[1].value
        $CurTable += ($table[1] -replace "
    ",",") -replace "" $CurTable += $table[2..($table.count-2)]|ForEach{$_ -replace "","," -replace ""} $CurTable | convertfrom-csv | export-csv "C:\Path\To\Output\$CurTableName.csv" -notype }

    That should output a CSV file for each table found. Such as table6.csv, table9.csv etc. If you wanted to output CSVs per HTML file you could wrap the entire thing in a ForEach loop like:

    ForEach($File in (Get-ChildItem "$Path\*.html")){
        Insert above code here
    }
    

    You would need to modify the $tables = line so that it was GC $file.fullname to that it would load up each file as it iterated through.

    Then just modify the Export-Csv to something like:

    $CurTable | convertfrom-csv | export-csv "C:\Path\To\Output\$($File.BaseName)\$CurTableName.csv" -notype
    

    So if you had Server01.html with 3 tables in it you would get a folder named Server01 with 3 CSV files in it, one for each table.

    提交回复
    热议问题