【问题标题】:Formatting columns of HTML output depending on their value根据值格式化 HTML 输出的列
【发布时间】:2016-07-22 10:29:30
【问题描述】:

我正在尝试将条件格式应用于一个简单的 PowerShell 函数的输出:

function Get-RamInfo {
    $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
    $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
    $memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
    $calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]

    $props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
        'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
        'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
        'Total Ram Available (%)'= $calc;
    }

    $obj = New-Object -TypeName PSObject -Property $props

    Write-Output $obj
}

这是数据的实际处理和应用的逻辑:

$frag3 = Get-RamInfo -ComputerName $computername |
         ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
         foreach {
             if ($_.'Total Ram Available (%)' -lt 20) {
                 $_ -replace "<tr>", '<tr class="warning">'
             } elseif ($_.'Total Ram Available (%)' -lt 10) {
                 $_ -replace "<tr>", '<tr class="danger">'
             } else{
                 $_
             }
         } | Out-String

我只希望 if 语句在应用逻辑时引用标有“可用总 RAM (%)”的列。例如。如果标有“可用总 RAM (%)”的列的值小于 20,则执行格式化,如果小于 10,则执行不同的格式化,对于其他任何内容,只需正常输出字符串而不进行格式化。

这是我得到的 HTML 输出:

<h2>Memory Info</h2>
<table>
<colgroup><col/><col/><col/><col/></colgroup>
<tr class="warning"><th>Total RAM (GB)</th><th>Total RAM In Use (GB)</th><th>Total RAM Available (GB)</th><th>Total Ram Available (%)</th></tr>
<tr class="warning"><td>31.96</td><td>7.44</td><td>24.52</td><td>77</td></tr>
</table>
  1. 我认为我没有正确引用单个列,因为它将格式应用于每个表格单元格。
  2. 看来我选择应用逻辑的方法相当复杂。

【问题讨论】:

    标签: html powershell powershell-5.0


    【解决方案1】:

    问题是一旦你将对象转换为 HTML,你将无法使用 if/else statemnet 来处理它的属性。我已经稍微编辑了你的脚本。它现在应该可以工作了。

    function Get-RamInfo {
        $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername
        $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername
        $memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
        $calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]
    
        $props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
            'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
            'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
            'Total Ram Available (%)'= $calc;
        }
    
        $obj = New-Object -TypeName PSObject -Property $props
    
        Write-Output $obj
    }
    
    $raminfo = Get-RamInfo -ComputerName $computername  | 
                    Select -Property *,@{
                                             name='class';e={
                                                 if ($_.'Total Ram Available (%)' -lt 20){'<tr class="warning">'}
                                                 elseif ($_.'Total Ram Available (%)' -lt 10){'<tr class="danger">'}
                                                 else{'<tr>'}
                                             }
                                         }
    
    $frag3 = $raminfo | Select 'Total RAM (GB)','Total RAM In Use (GB)','Total RAM Available (GB)','Total Ram Available (%)' | 
             ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
                foreach { $_ -replace '<tr>',$($raminfo.class)
                } | Out-String
    

    【讨论】:

      【解决方案2】:

      当您的if 语句看到数据时,您不再拥有您要检查的对象和属性。请改用正则表达式和 switch 语句从 HTML 字符串中提取数值:

      $re = '<tr>(<td>[0-9.]+?</td><td>[0-9.]+?</td><td>([0-9.]+?)</td>)'
      
      ... | ForEach-Object {
        if ($_ -match $re) {
          switch ([int]$matches[2]) {
            {$_ -lt 20} { $_ -replace $re, '<tr class="warning">$1' }
            {$_ -lt 10} { $_ -replace $re, '<tr class="critical">$1' }
            default     { $_ }
          }
        }
      } | ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多