Powershell 将选项卡式文本文件内容转换为良好格式 HTML Table

Powershell convert tabbed text file content to well formatted HTML Table

我想在powershell中将一个文本文件处理成格式良好的HTML table,下面是文件内容-

Machine ID  Proc Name   Proc Inst  Status         Comment             
----------------------------------------------------------------------
1           BG          1          RUNNING        BG process started  
1           BG          2          RUNNING        BG process started  
1           BG          3          RUNNING        BG process started  
1           BGPREDICT   1          RUNNING        BG process started  
1           DLMGR       1          RUNNING        DLMGR process started
1           IAPJMS      1          NOT RUNNING                        
1           RPCBG       1          RUNNING        RPCBG process started
1           RPC_TCP_LI  1          RUNNING        RPC listener process started
1           RPC_UDP_LI  1          RUNNING        RPC listener process started
1           SPO         1          RUNNING        SPO Server process started
1           SPO         2          RUNNING        SPO Server process started
1           WIS         1          RUNNING        WIS process started 
1           WIS         2          RUNNING        WIS process started 
1           WIS         3          RUNNING        WIS process started 
1           WIS         4          RUNNING        WIS process started 
1           WIS         5          RUNNING        WIS process started 
1           WIS         6          RUNNING        WIS process started 
1           WISMBD      1          RUNNING        WISMBD process started
1           WISMBD      2          RUNNING        WISMBD process started
1           WQS         1          RUNNING        WQS process started 


Timed out waiting for system status response.

我无法转换,因为它们看起来与 HTML Table 形式相同,但是通过使用下面的代码片段-

将每行的所有内容都放在单个单元格中
Get-Content $env:TEMP\stat.txt|ConvertTo-HTML -Property
   @{Label='Text';Expression={$_}}
来自文本文件的

Header 在 HTML Table 中应该相同 在休息的情况下,每个单元格都应该分别对待

请帮我实现这个目标。

此脚本使用多个 RegEx 替换来解析一个对象以形成一个 csv,然后使用 ConvertFrom-Csv cmdlet:

$table= (gc .\sample.txt -raw ) -replace "-{10,100}`r?`n" -replace "`r?`n *`r?`n"
$table -split "`r?`n" | %{
    If ($_ -notmatch 'Timed out'){
        "`"{0}`"" -f ($_ -replace " {2,15}",'","')
    }
}|ConvertFrom-Csv | ConvertTo-html | Set-Content .\Sample.html -Encoding UTF8

样本ConvertFrom-Csv输出

Machine ID Proc Name  Proc Inst Status      Comment
---------- ---------  --------- ------      -------
1          BG         1         RUNNING     BG process started
1          BG         2         RUNNING     BG process started
...snip..

通过管道传输到 ConvertTo-html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Machine ID</th><th>Proc Name</th><th>Proc Inst</th><th>Status</th><th>Comment</th></tr>
<tr><td>1</td><td>BG</td><td>1</td><td>RUNNING</td><td>BG process started</td></tr>
<tr><td>1</td><td>BG</td><td>2</td><td>RUNNING</td><td>BG process started</td></tr>
...snip...

并保存到您可以在浏览器中查看的文件中: