I have a group of simple PowerShell functions that retrieve system statistics, convert them to HTML fragments & then join it all together into one big HTML file at the end.
I'm having a problem with some specific functions, whereas others appear to be working fine although I'm using exactly the same principle.
This retrieves the system information:
function Get-ApplicationLogs {
$query = '<QueryList>
<Query Id="0" Path="Application">
<Select Path="Application">*[System[(Level=1 or Level=2 or Level=3) and TimeCreated[timediff(@SystemTime) <= 86400000]]]</Select>
</Query>
</QueryList>'
$logs = Get-WinEvent -ComputerName $ComputerName -ErrorAction SilentlyContinue -FilterXml $query | Sort-Object {$_.Id}
foreach ($log in $logs) {
$props = [ordered]@{'Id'=$log.Id;
'Error Type'=$log.LevelDisplayName;
'Provider Name'=$log.ProviderName;
'Timestamp'=$log.TimeCreated;
'Message'=$log.Message;
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
}
This applies the logic to the function and applies the conditional formatting for the class of the output HTML code:-
$applogs = Get-ApplicationLogs -computername $computername |
Select -Property *,@{
name='class';e={
if ($_.'Error Type' -match 'Error' -or 'Critical') {
'<td class="danger">'
} elseif ($_.'Error Type' -match 'Warning') {
'<td class="warning">'
} else {
'<td>'
}
}
}
This binds it all together and executes the td class tag replacement:
$frag8 = $applogs | Select 'ID','Error Type','Provider Name','Timestamp','Message' |
ConvertTo-Html -As Table -Fragment -PreContent '<hr>', '<h3>Application Logs</h3>' |
foreach { $_ -replace '<td>',$($applogs.class) } |
Out-String
But for some reason I get a very skewed HTML output, it seems to generate more tags than required for each value:-
<hr>
<h3>Application Logs</h3>
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Id</th><th>Error Type</th><th>Provider Name</th><th>Timestamp</th><th>Message</th></tr>
<tr>
<td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">10021</td>
<td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">Warning</td>
<td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">Windows Server Update Services</td>
<td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">08/08/2016 04:37:42</td>
<td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger"> <td class="danger">The catalog was last synchronized successfully 1 or more days ago.</td>
</tr>