首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换到-HTML <td>类

转换到-HTML <td>类
EN

Stack Overflow用户
提问于 2016-08-08 09:53:51
回答 1查看 1.1K关注 0票数 0

我有一组简单的PowerShell函数,用于检索系统统计信息,将它们转换为HTML片段&然后将它们合并到最后的一个大型HTML文件中。

我在一些特定的函数上有问题,而其他的功能看起来很好,尽管我使用的是完全相同的原则。

这将检索系统信息:

代码语言:javascript
复制
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) &lt;= 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代码的类应用条件格式设置:-

代码语言:javascript
复制
$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类标记替换:

代码语言:javascript
复制
$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输出,它生成的标记似乎比每个值所需的要多:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2016-08-08 12:06:05

{ $_ -replace‘,$($applogs.class) }

上面的语句将把每个<td>元素替换为$applogs中所有记录的class值,因此,如果<td>元素包含多个记录,则$applogs元素的乘数。

在尝试添加格式化信息的时候,您不再拥有原始对象,因此需要解析这些信息,以便根据您创建的HTML字符串进行格式化。当我回答你的原始问题时我已经告诉过你了。解决方案(基本上)也是一样的。

代码语言:javascript
复制
$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'并不像您所期望的那样工作。表达式的计算方式如下:

代码语言:javascript
复制
$_.'Error Type' -match 'Error' -or 'Critical'
($_.'Error Type' -match 'Error') -or ('Critical')
($_.'Error Type' -match 'Error') -or ($true)
$true

因为$true在PowerShell中,如果至少有一个操作数是$true,则布尔或运算的计算结果为$true

如果要继续使用-match运算符,可以在正则表达式中使用替换:

代码语言:javascript
复制
$_.'Error Type' -match 'Error|Critical'

您还可以进行两个单独的-eq比较:

代码语言:javascript
复制
$_.'Error Type' -eq 'Error' -or $_.'Error Type' -eq 'Critical'

或检查Error Type值是否包含在有效值数组中:

代码语言:javascript
复制
'Error', 'Critical' -contains $_.'Error Type'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38826124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档