ConvertTo-Html cmdlet如何添加列到输出

时间:2016-06-28 08:59:12

标签: html css powershell

我目前正在开发PowerShell脚本,该脚本使用HTML cmdlet将文本文件报告转换为ConvertTo-Html文件中的一系列表格。文本文件中的一个部分如下所示:


+-------------------------------------------------------------------------+
¦Operating System Info:                                                   ¦
+-------------------------------------------------------------------------+

Operating System:   Microsoft Windows Server 2008 R2 Standard  
Service Pack:       Service Pack 1
Product Type:       Server
Product ID:         55041-266-0358183-84672
Architecture:       64-bit
Install Date:       2013-04-11 11:58:26  
Encryption Level:   256 bit
Local Time:         2016-02-18 08:06:58  
Last Bootup Time:   2015-08-13 07:10:00  
Total Physical Memory:  18863924 KB
Free Physical Memory:   14811704 KB
Total Virtual Memory:   37725992 KB
Free Virtual Memory:    33103616 KB
Data Execution Prev:    DEP is enabled for Windows services only (OptIn - Windows Default)

+-------------------------------------------------------------------------+
¦Computer Info:                                                           ¦
+-------------------------------------------------------------------------+

Computer Name:      COMPNAME
DNSHostName:        COMPNAME
Pinging aaccms2 [172.20.78.132] with 32 bytes of data:
Reply from 172.20.78.132: bytes=32 time

![Service Pack: Service Pack 1

而不是保持它之前的间距。

所以我的问题是,有什么方法可以让表格在某个字符或一组字符之后拆分成一个新列,比如说“:”?我还做了一些测试,在将文件写入HTML之前将其附加到文件中,从而产生如下内容:

Total Physical Memory: 16000000 KB: PASS

所以,如果我可以把它分成三列,一个用于标题,一个用于值,一个用于传递失败,甚至更好。我的PowerShell转换如下所示:

foreach ($Line in $Table) {
    # Converts each line to HTML output, does not include lines that have
    # text file spacers, blank lines, or titles in the line (titles added
    # later)

    if ($Line -ne "+-------------------------------------------------------------------------+" `
     -and $Line -ne "+-----------------------------------------------------------------------------------+" `
     -and $Line -ne "" `
     -and $Line -ne $Titles[$count]) {
        $Object = New-Object -TypeName PSObject
        Add-Member -InputObject $Object -Type NoteProperty -Name AACC -Value $Line
        $Document += $Object
    }
}

$CurrentTitle = $Titles[$count]
$Frag += $Document |
    ConvertTo-Html -As TABLE -Fragment -PreContent "<h2 align = center id = $CurrentTitle;> $CurrentTitle  </h2>" |
    Out-String   # Adds the new HTML fragment (table) to the frag array

ConvertTo-Html -Title $FileName -Head $head -PostContent $Frag -Property AACC -Body $Body |
    Out-File $TargetFile   # Outputs the new HTML file

我设置表格布局的$head变量看起来像

$Title = "<title> $Filename Report</title>"

$CSSStyle = @'
<style>
ul {
  padding-left: 0px;
}
body { background-color:White;
  font-family:Tahoma;
  font-size:12pt;
}
td, th {border:1px solid black;} 
th {
  color: black;
  background-color:#C11B17;
}
td { border-width: 1px;padding: 0px;border-style: solid;border-color: black; }
TR:Hover TD { Background-Color: #C1D5F8; }
table, tr, td, th { align:left; padding: 2px; margin: 0px; }
table { width:100% }
table { margin-left:0px; }
</style>
'@

$Head = $Title + $CSSStyle

2 个答案:

答案 0 :(得分:4)

我会开始使用简单的正则表达式过滤所有不必要的行:

Get-Content 'your_file' | Where { $_.Trim() -notmatch '[+¦]|^$' }

<强>输出继电器:

Operating System:        Microsoft Windows Server 2008 R2 Standard  
Service Pack:            Service Pack 1
Product Type:            Server
Product ID:              1234-5678
Architecture:            64-bit
Install Date:            2013-01-01 01:01:01  
Encryption Level:        256 bit`
Total Physical Memory:   16000000 KB

然后使用另一个正则表达式捕获键和值并从中创建PSCustomObject

Get-Content 'your_file' | Where { $_.Trim() -notmatch '[+¦]|^$' } | foreach { 
    $regexMatch = [regex]::Match($_.Trim(), '(?<Key>[^:]+):\s+(?<Value>.+)')
    [PSCustomObject]@{
        Key = $regexMatch.Groups['Key'].Value
        Value = $regexMatch.Groups['Value'].Value
    }    
} 

<强>输出:

Key                   Value                                    
---                   -----                                    
Operating System      Microsoft Windows Server 2008 R2 Standard
Service Pack          Service Pack 1                           
Product Type          Server                                   
Product ID            1234-5678                                
Architecture          64-bit                                   
Install Date          2013-01-01 01:01:01                      
Encryption Level      256 bit`                                 
Total Physical Memory 16000000 KB   

现在您可以将输出传递给ConvertTo-Html cmdlet:

Get-Content 'your_file' | Where { $_.Trim() -notmatch '[+¦]|^$' } | foreach { 
    $regexMatch = [regex]::Match($_.Trim(), '(?<Key>[^:]+):\s+(?<Value>.+)')
    [PSCustomObject]@{
        Key = $regexMatch.Groups['Key'].Value
        Value = $regexMatch.Groups['Value'].Value
    }    
} |  ConvertTo-Html -Title $FileName -Head $head | out-file $TargetFile

<强>输出: enter image description here

编辑您的评论:

我会开始分开各个部分:

$content = Get-Content 'your_file'
$headings = $content | sls '\s*¦' | 
    select LineNumber, @{l="heading"; e={[regex]::Match($_.Line, '¦([^:]+)').Groups[1].Value}}, Content

for ($i = 0; $i -lt $headings.Count; $i++)
{
    if ($i +1 -lt $headings.Count)
    {
        $headings[$i].Content = ($content[$headings[$i].LineNumber .. $headings[$i +1].LineNumber]).Trim() -notmatch '[+¦]|^$'
    }
    else # last entry
    {
        $headings[$i].Content = ($content | select -skip $headings[$i].LineNumber).Trim() -notmatch '[+¦]|^$'
    }    
}

这将为您提供标题($headings | select heading, Content)的标题和内容:

heading               Content                                                                                                                                                             
-------               -------                                                                                                                                                             
Operating System Info {Operating System:   Microsoft Windows Server 2008 R2 Standard, Service Pack:       Service Pack 1, Product Type:       Server, Product ID:         55041-266-035...
Computer Info         {Computer Name:      COMPNAME, DNSHostName:        COMPNAME, Pinging aaccms2 [172.20.78.132] with 32 bytes of data:, Reply from 172.20.78.132: bytes=32 time} 

现在你要做的就是组合两个脚本。

答案 1 :(得分:2)

具有所有所需属性的自定义对象将更易于操作和导出。这可以帮助您获得所需的列:

$inputLines = Get-Content "input.txt"

#create custom object
$result = New-Object -TypeName PSCustomObject

$inputLines |
    #skip lines containing ¦ or -----
    Where-Object { $_ -NotMatch "\¦|-----" } | ForEach-Object {   

    $lineArray = $_.Split(":")

    #add property to custom object
    if($lineArray[0]) {

        $result | Add-Member -MemberType NoteProperty `
            -Name $lineArray[0].Trim() `
            -Value (($lineArray | Select-Object -Skip 1) -join ":").Trim()

    }
}

#export to HTML file
$result | ConvertTo-Html -As List | Out-File "output.html"

注意:不需要;之后的$CurrentTitle(它将在HTML代码中输出)。

注2:最终学分归Martin Brel所说,他亲切地指出了一个错误......然后帮我解决了这个问题。