Ich muss viele Reports per E-Mail verschicken. Diese sollten trotzdem vernünftig aussehen, OHNE ein externes Style-Sheet zu benötigen. Darum habe ich die Funktion geschrieben, welche ein Array zu einer HTML-Tabelle konvertiert und dieses leserlich darstellt.
Es ist auch möglich nur bestimmte Attribute anzeigen zu lassen. Diese werden dann in der Reihenfolge aufgelistet wie sie unter “NoteProperties” aufgeführt werden.
function Convert-Table2HTML { <# .SYNOPSIS Convert a PowerShell-Table to a HTML-Table. .DESCRIPTION The Convert-Table2HTML cmdlet converts a PowerShell-Table to a HTML-Table in order to send it via email and have it available directly within the email. .PARAMETER Table Specifies the PowerShell-Table. .PARAMETER Table This optional parameter specifies the NoteProperties you want to pass. NoteProperties must be comma-seperated and will appear in the listed order. It doesn't matter if there a blank spaces before or after the comma. If you don't define the NoteProperties, ALL available NoteProperties will be used in alphabetical order. .EXAMPLE PS C:\> Convert-Table2HTML -Table $ExampleTable PS C:\> Convert-Table2HTML -Table $ExampleTable -NoteProperties "VMName, ClusterName" .NOTES Author: Joachim Armbruster Blog : http://PowerShell24.de/ #> Param ( [parameter(Mandatory=$true)]$Table, $NoteProperties ) $Farbe1 = "bgcolor='#c5c5c5'" $Farbe2 = "bgcolor='#f0f0f0'" if(!$NoteProperties) { $NoteProperties = $Table | Get-Member -Type *Property* $CustomNoteProperty = $false } else { $Array = $NoteProperties -Split "," $NoteProperties = @() foreach($Iterator in $Array) { While($Iterator -like "* ") { $Iterator = $Iterator.SubString(0, $Iterator.Length-1) } While($Iterator -like " *") { $Iterator = $Iterator.SubString(1, $Iterator.Length-1) } $NoteProperties += $Iterator } $CustomNoteProperty = $true } $html = "<table border='1'>" $html += "<tr>" foreach($NoteProperty in $NoteProperties) { if($CustomNoteProperty) { $NotePropertyName = $NoteProperty } else { $NotePropertyName = $NoteProperty.Name } $html += "<th>$NotePropertyName</th>" } $html += "</tr>" $FarbenCounter = 0 for ($i = 0; $i -lt $Table.Count; $i++) { if(($FarbenCounter % 2) -eq 0) { $bgcolor = $Farbe1 }else { $bgcolor = $Farbe2 } $html += "<tr " + $bgcolor + ">" foreach($NoteProperty in $NoteProperties) { if($CustomNoteProperty) { $NotePropertyName = $NoteProperty } else { $NotePropertyName = $NoteProperty.Name } $Eintrag = $Table[$i].$NotePropertyName $html += "<td>$Eintrag</td>" } $html += "</tr>" $FarbenCounter++ } $html += "</table>" return $html }