我目前正在使用ConvertTo-Html
cmdlet處理PowerShell腳本,該腳本可將文本文件報告轉換爲HTML
文件中的一系列表格。在文本文件中的部分看起來像這樣:ConvertTo-Html cmdlet如何將列添加到輸出
+-------------------------------------------------------------------------+ ¦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
而不是將前面有間距的。
所以我的問題是,有什麼辦法可以讓表格分裂成一個字符或一組字符後的新列,比如說「:」?我也讓之前被寫入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
正則表達式捕獲組名......太有光澤了。幾天前閱讀,確實使捕獲組更容易使用。非常好。 – sodawillow
感謝您的幫助,但是您是否知道如何讓這個功能適用於多個表格?我遇到的主要問題是將此應用於單個表片段(我在上述腳本中使用-frag參數)。如果文本文件中只有一個表格,我可以使用它,但有很多,包括計算機信息,驅動器信息等等。 – MacMixer13
我以爲你可以自己做這個。如果在示例中添加另一個表格,我會查看一下。 –