格式化HTML输出的列取决于它们的值 [英] Formatting columns of HTML output depending on their value

查看:137
本文介绍了格式化HTML输出的列取决于它们的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将条件格式应用到一个简单的PowerShell函数的输出中:
$ b

 函数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);
'使用中的总RAM(GB)'={0:N2}-f([math] :: Round($ memcalc,2));
'可用RAM总数(GB)'={0:N2}-f($ os.freephysicalmemory / 1MB);
'可用总Ram(%)'= $ calc;

$ b $ 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(%)'-tt 20){
$ _ -replace< tr>,'< tr class = ($ _。'Total Ram Available(%)'-lt 10){
$ _ -replace< tr>,'< tr class =危险>'
} else {
$ _
}
} | Out-String

我只希望if语句引用标记为Total RAM Available(%) 应用逻辑时。例如。如果标记为Total RAM Available(%)的列的值小于20,则执行格式化(如果小于10,则执行不同的格式化),对于其他任何情况,只需正常输出字符串而不格式化。 p>

以下是我得到的HTML输出:

 < h2>存储器信息< / h2> 
< table>
< colgroup>< col />< col />< col />< col />< / colgroup> (GB)第(总共RAM)(GB)第(总)RAM使用中的总数(GB)第(th)总RAM可用(GB)< / th>第>可用的总可用的(%)< / th>< / tr>
< tr class =warning>< td> 31.96< / td>< td> 7.44< / td>< td> / TD>< / TR>
< / table>




  1. 我不认为我正确地引用单列它将格式应用于每个表格单元格。

  2. 看来我选择应用逻辑的方法非常复杂。

解决方案

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

 函数Get-RamInfo {
$ os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ computername
$ cs = Get-WmiObject -Class Win32_ComputerSystem -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);
'使用中的总RAM(GB)'={0:N2}-f([math] :: Round($ memcalc,2));
'可用RAM总数(GB)'={0:N2}-f($ os.freephysicalmemory / 1MB);
'可用总Ram(%)'= $ calc;

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

Write-Output $ obj
}

$ raminfo = Get-RamInfo -ComputerName $ computername |
选择-Property *,@ {
name ='class'; e = {
if($ _。'Total Ram Available(%)'-t 20){'< tr
elseif($ _。'Total Ram Available(%)'-lt 10){'< tr class =danger>'}
else { '< tr>'}
}
}

$ frag3 = $ raminfo |选择'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


I'm trying to apply conditional formatting to the output of a simple PowerShell function:

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
}

Here is the actual processing of the data and the logic applied:

$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

I only want the if statement to reference the column labelled "Total RAM Available (%)" when applying the logic. E.g. If the value of the column labelled "Total RAM Available (%)" is less than 20, then execute the formatting, if it's less than 10, then execute different formatting, for anything else just output the string as normal without formatting.

Here is the HTML output I get:

<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. I don't think I'm referencing the single column correctly as it's applying the formatting to every table cell.
  2. It seems that the method I'm choosing to apply the logic is quite convoluted.

解决方案

The problem is that once you have converted object to HTML you wont be able to use if/else statemnet to its properties. I have edited your script a little. It should work now.

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

这篇关于格式化HTML输出的列取决于它们的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆