根据HTML值的值来格式化HTML输出列

时间:2016-07-21 10:22:21

标签: html powershell powershell-v5.0

我正在尝试将条件格式应用于简单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语句在应用逻辑时引用标记为“Total RAM Available(%)”的列。例如。如果标记为“Total RAM Available(%)”的列的值小于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. 我选择应用逻辑的方法似乎很复杂。

2 个答案:

答案 0 :(得分:0)

当您的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     { $_ }
    }
  }
} | ...

答案 1 :(得分:0)

问题是,一旦将对象转换为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