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

时间:2018-06-03 21:30:23

标签: powershell powershell-v2.0

我想在powershell中处理文本文件到格式良好的HTML表,下面是文件内容 -

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表格形式相同,但通过使用以下代码段将所有内容都放在每行的单个单元格中 -

Get-Content $env:TEMP\stat.txt|ConvertTo-HTML -Property
   @{Label='Text';Expression={$_}}

来自文本文件的标题应该在HTML表格中相同,并且每个单元格分别对待

请帮助我实现这一目标。

1 个答案:

答案 0 :(得分:2)

此脚本使用多个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...

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

enter image description here