Powershell 中的条件 HTML 样式 [英] Conditional HTML Style in Powershell

查看:19
本文介绍了Powershell 中的条件 HTML 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本来显示我们的 vCenter 的虚拟机磁盘状态.我希望能够突出显示可用空间%"小于 20 的行.我找不到隔离此变量并为其添加单独样式的方法.我的代码如下:

I have a script to display VM disk status for our vCenter. I would like to be able to highlight the rows in which "Free Space%" is less than 20. I can't find a way to isolate this variable and add a separate styling for it. My code is below:

Add-PSSnapin vmware.vimautomation.core
Connect-viserver "servername"

$editdate = Get-Date
$diskReport = "locationofreport.html"
$servers = get-vm | Sort name

#HTML Styling
$a = "<title>VM Disk Status</title>
<style>
 Body{background:#003366; height:100%; width:100%; padding:0;}
 Table{text-align:center; width:850px; padding-bottom:10px; border-width:1px; border-style:solid; border-color:black; margin: 0 auto; font-family: Georgia;}
 TH{border-width:1px;padding:5px; border-style:solid; border-color:black; background-color:grey; color:#FFDB4D;}
 TD{border-width:1px; padding:2px; border-style: solid; border-color:black; background-color:grey(this is the element I want to vary); color:white;}
 P{font-size:3em; text-align:center; font-family:Georgia; color:#FFDB4D}
</style>"
$b = "<p>VM Check Disk Page<br />Last Edited on $editdate</p>"

#Start Script
If (Test-Path $diskreport)
   {Remove-Item $diskreport}

$b | Add-Content $diskreport

ForEach ($vm in $servers) {
   ($vm.extensiondata.Guest.Disk |
      Select @{N="Name";E={$vm.name}},
      Diskpath,
      @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}},
      @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace/ 1MB)}},
      @{N="Free Space %";E={[math]::Round(((100*($_.FreeSpace))/($_.Capacity)),0)}}
   ) | ConvertTo-Html -head $a | add-content $diskreport
}

如果有人有任何见解,那就太好了.

If anyone had any insight, that would be great.

推荐答案

通过按照 Matt 的建议手动输出表格,并使用一些 CSS 类(请不要使用内联样式 (style="..." in theHTML 标签),在 CSS 样式表中添加类,因为你已经有了一个),你可以实现这一点.

By manually outputting the table as advised by Matt, and using some CSS classes (please don't use inline styling (style="..." in the HTML tags), add classes in the CSS stylesheet, as you already have one), you can achieve this.

另外,检查我对 CSS 所做的更改,它有点短(这样更易读).

Also, check the changes I made to the CSS, it's a bit shorter (and more readable like this).

我在工作中运行了一个类似的脚本,它在网页上显示 CSV 文件的内容,根据值动态添加类.

I have a similar script running at work that displays the content of a CSV file on a webpage, adding classes on the fly depending of the values.

CSS:

<style>

body {
  background: #003366;
  height: 100%;
  width: 100%;
  padding: 0;
}

table{
  text-align: center;
  width: 850px;
  padding-bottom: 10px;
  border: 1px solid black;
  margin: 0 auto; 
  font-family: Georgia;
}

th {
  border: 1px solid black;
  padding: 5px; 
  background-color: grey;
  color: #FFDB4D;
}

td {
  border: 1px solid black;
  padding: 2px;
  background-color: grey;
  color: white;
}

p {
  font-size: 3em;
  text-align: center;
  font-family: Georgia;
  color: #FFDB4D
}

tr.under-20 td {
  /* this targets cells in rows having class="under-20" */
  background-color: red;
  font-weight: bold;
}

</style>

使用 for/foreach 循环,可以输出表结构,基本上是:

With a for / foreach loop, you can output the table structure which is basically :

HTML:

<table> <!-- table start -->
  <tr> <!-- row start -->
    <th>First name</th> <!-- header cell -->
    <th>Last name</th> <!-- header cell -->
  </tr> <!-- row end -->
  <tr> <!-- row start -->
    <td>John</td> <!-- normal cell -->
    <td>Doe</td> <!-- normal cell -->
  </tr> <!-- row end -->
  <tr classs="under-20"> <!-- cells in this row will have a red background based on the stylesheet above -->
    <td>Jane</td>
    <td>Doe</td>
  </tr>
</table> <!-- table end -->

生成表格的PS代码示例(有不懂的可以问):

Example of PS code to generate the table (feel free to ask if you don't get it) :

$title = "page title goes here"

$stylesheet = "/* CSS goes here */

            body {
                font-family: Arial, sans-serif
            }

            th {
                background-color: grey;
            }

            tr.under-20 td {
                background-color: red;
                color: white;
            }"

$html = "<html>
    <head>
        <title>$title</title>
        <style>`n`
            $stylesheet`n`
        </style>
    </head>
    <body>"

foreach ($vm in $servers) {
    $html += "`n`t`t<table>`
        `n`t`t`t<tr>`
            `n`t`t`t`t<th>Name</th>`
            `n`t`t`t`t<th>Path</th>`
            `n`t`t`t`t<th>Capacity</th>`
            `n`t`t`t`t<th>Free space (MB)</th>`
            `n`t`t`t`t<th>Free space (%)</th>`
        `n`t`t`t</tr>"

    foreach($disk in $vm.extensiondata.Guest.Disk) {
        $name = $vm.name
        $diskpath = $disk.Diskpath
        $capacity = [math]::Round($disk.Capacity/ 1MB)
        $freeSpace = [math]::Round($disk.FreeSpace/ 1MB)
        $freePercent = [math]::Round(((100 * $disk.FreeSpace) / $disk.Capacity), 0)

        if($freePercent -le 20) {
            $rowclass = "under-20"
        } else {
            $rowclass = ""
        }

        $html += "`n`t`t`t<tr class=`"$rowclass`">`
            `n`t`t`t`t<td>$name</td>`
            `n`t`t`t`t<td>$diskpath</td>`
            `n`t`t`t`t<td>$capacity</td>`
            `n`t`t`t`t<td>$freeSpace</td>`
            `n`t`t`t`t<td>$freePercent</td>`
        `n`t`t`t</tr>"
    }
    $html += "`n`t`t</table>"
}

$html += "`n`t</body>`n</html>"

$html | Out-File "diskreport.html"

转义的制表符和换行符提供代码缩进,字符串中的尾随反引号是有意的.

Escaped tabs and newlines provide code indentation, trailing backticks in the strings are intended.

这篇关于Powershell 中的条件 HTML 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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