2016-06-28 31 views
3

我目前正在使用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 

![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 

回答

3

我會開始使用一個簡單的正則表達式來過濾所有不必要的行:

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} 

現在您只需將兩個腳本合併即可。

+0

正則表達式捕獲組名......太有光澤了。幾天前閱讀,確實使捕獲組更容易使用。非常好。 – sodawillow

+0

感謝您的幫助,但是您是否知道如何讓這個功能適用於多個表格?我遇到的主要問題是將此應用於單個表片段(我在上述腳本中使用-frag參數)。如果文本文件中只有一個表格,我可以使用它,但有很多,包括計算機信息,驅動器信息等等。 – MacMixer13

+0

我以爲你可以自己做這個。如果在示例中添加另一個表格,我會查看一下。 –

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:最後的信用去馬丁·布蘭德誰好心指出一個錯誤......然後幫我修復它。

+0

'分裂'可能不是一個好主意,在這裏考慮'安裝日期'。 –

+0

...我希望你在我寫代碼時坐在我旁邊。我不得不仔細檢查輸出HTML,因爲我不會因爲這個而得到一個錯誤... DAMN!至少我得到了一個投票:) – sodawillow

+0

:-)。你可以採用你的答案來使用'-Value $ lineArray [1 .. -1]' –