我有一组简单的PowerShell函数,用于检索系统统计信息,将它们转换为HTML片段&然后将它们合并到最后的一个大型HTML文件中。
我在一些特定的函数上有问题,而其他的功能看起来很好,尽管我使用的是完全相同的原则。
这将检索系统信息:
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
}
}这将逻辑应用于函数,并为输出HTML代码的类应用条件格式设置:-
$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>'
}
}
}它将所有这些绑定在一起,并执行td类标记替换:
$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但是由于某种原因,我得到了一个非常倾斜的HTML输出,它生成的标记似乎比每个值所需的要多:
<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>发布于 2016-08-08 12:06:05
{ $_ -replace‘,$($applogs.class) }
上面的语句将把每个<td>元素替换为$applogs中所有记录的class值,因此,如果<td>元素包含多个记录,则$applogs元素的乘数。
在尝试添加格式化信息的时候,您不再拥有原始对象,因此需要解析这些信息,以便根据您创建的HTML字符串进行格式化。当我回答你的原始问题时我已经告诉过你了。解决方案(基本上)也是一样的。
$re = '<tr><td>.*?</td><td>(Error|Warning)</td>'
... | ForEach-Object {
if ($_ -match $re) {
$type = $matches[1]
$_ -replace '<td>', "<td class='$type'>"
} else {
$_
}
} | ...此外,正如@wOxxOm在对你问题的评论中提到的那样,逻辑操作$_.'Error Type' -match 'Error' -or 'Critical'并不像您所期望的那样工作。表达式的计算方式如下:
$_.'Error Type' -match 'Error' -or 'Critical'
($_.'Error Type' -match 'Error') -or ('Critical')
($_.'Error Type' -match 'Error') -or ($true)
$true因为$true在PowerShell中,如果至少有一个操作数是$true,则布尔或运算的计算结果为$true。
如果要继续使用-match运算符,可以在正则表达式中使用替换:
$_.'Error Type' -match 'Error|Critical'您还可以进行两个单独的-eq比较:
$_.'Error Type' -eq 'Error' -or $_.'Error Type' -eq 'Critical'或检查Error Type值是否包含在有效值数组中:
'Error', 'Critical' -contains $_.'Error Type'https://stackoverflow.com/questions/38826124
复制相似问题