有选择地格式化PowerShell管道中的数据并以HTML格式输出的技术

时间:2010-12-30 01:06:20

标签: html css xml powershell

假设您想要对powershell的某些表格输出进行一些奇特的格式化,目标是html(对于Web服务器,或者通过电子邮件发送)。例如,假设您希望某些数值具有不同的背景颜色。随你。我可以想到两种可靠的编程方法来实现这一点:使用XSLT输出XML和转换,或输出HTML并使用CSS进行装饰。

XSLT可能是两者中较难的(我说是因为我不知道),但是从我记忆中的一点点来看,它具有能够嵌入上述想法的选择标准(xpath?)的好处。格式。另一方面,CSS需要帮助。如果你想要特殊处理某个单元格,那么你需要将它与兄弟姐妹的类别,id或类似的东西区分开来。 PowerShell本身并没有办法做到这一点,所以这意味着解析HTML,因为它离开convertto-html并添加,例如,“强调”类:

<td class="emphasis">32MB</td>

我不喜欢所需文本解析的想法,特别是考虑到我希望能够以某种方式强调在Powershell中强调HTML之前需要强调的内容。

XSLT是最好的方法吗?有关于如何在将convertto-html或其他方式提出想法后标记HTML的建议吗?

4 个答案:

答案 0 :(得分:7)

如何使用JQuery,并使用JQUery脚本和一些样式插入标题,如:

Get-Process | ConvertTo-Html -Head @'

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

   $("table tr td:nth-child(" + ( $("table th:contains('WS')").index() + 1 ) + ")").each(function() {
      if($(this).html() > 209715200) { // 200MB
         $(this).css("background", "red" );
      } else if($(this).html() > 20971520) { // 20MB
         $(this).css("background", "orange" );
      } else if($(this).html() > 10485760) { // 10MB
         $(this).css("background", "yellow" );
      }
   });

})
</script>

'@ | Out-File procs.html; ii .\procs.html

答案 1 :(得分:7)

嘿,我想出了另一个我更喜欢的答案。这个不依赖于支持JavaScript的浏览器...

Add-Type -AssemblyName System.Xml.Linq

# Get the running processes to x(ht)ml
$xml = [System.Xml.Linq.XDocument]::Parse( "$(Get-Process | ConvertTo-Html)" )

# Find the index of the column you want to format:
$wsIndex = (($xml.Descendants("{http://www.w3.org/1999/xhtml}th") | Where-Object { $_.Value -eq "WS" }).NodesBeforeSelf() | Measure-Object).Count

# Format the column based on whatever rules you have:
switch($xml.Descendants("{http://www.w3.org/1999/xhtml}td") | Where { ($_.NodesBeforeSelf() | Measure).Count -eq $wsIndex } ) {
   {200MB -lt $_.Value } { $_.SetAttributeValue( "style", "background: red;"); continue } 
   {20MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: orange;"); continue } 
   {10MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: yellow;"); continue } 
}
# Save the html out to a file
$xml.Save("$pwd/procs2.html")

# Open the thing in your browser to see what we've wrought
ii .\procs2.html

是否有特殊徽章可以自行窃取“标记为答案”? ; - )

答案 2 :(得分:5)

答案 3 :(得分:1)

作为一些github讨论的结果,我使用.where()方法为PowerShell 4/5/6更新了这个,从而消除了对管道的依赖。缓慢的部分是生成HTML,而实际的XML操作只需要大约200毫秒。

Add-Type -AssemblyName System.Xml.Linq

# Get the running processes to x(ht)ml. This is *SLOW*.
$xml = [System.Xml.Linq.XDocument]::Parse([string] (Get-Process | ConvertTo-Html))

# Find the index of the column you want to format:
$wsIndex = $xml.Descendants("{http://www.w3.org/1999/xhtml}th").
    Where{$_.Value -eq "WS" }.
        NodesBeforeSelf().
            Count

# Format the column based on whatever rules you have:
switch($xml.Descendants("{http://www.w3.org/1999/xhtml}td").Where{@($_.NodesBeforeSelf()).Count -eq $wsIndex} ) {
   {200MB -lt $_.Value } { $_.SetAttributeValue( "style", "background: red;"); continue } 
   {20MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: orange;"); continue } 
   {10MB  -lt $_.Value } { $_.SetAttributeValue( "style", "background: yellow;"); continue }
}

# Save the html out to a file
$xml.Save("$pwd/procs2.html")

# Open the thing in your browser to see what we've wrought
ii .\procs2.html