[英]Powershell, Linq, Html: change background to entire ror
以下代碼能夠將背景更改為某個單元格,但是我需要將背景顏色更改為整行。
這是一種方法嗎?
TNX
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("c:\procs2.html")
# Open the thing in your browser to see what we've wrought
ii c:\procs2.html
此處的邏輯(可行的)解決方案是獲取循環中每個td
單元的父元素,即整個行( tr
)。
替換三個:
$_.SetAttributeValue(
同
$_.Parent.SetAttributeValue(
您應該花一些時間來實際學習PowerShell,也許需要一點HTML並通過Google進行搜索。 如果您搜索了XDocument
的Descendants
方法,則將看到它返回一個XElement ,MSDN表示該元素具有Parent
屬性。
更新:這是您的腳本,其中包含我在三個位置的單個更新。 它在這里完美工作。 整行變色。
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 } { $_.Parent.SetAttributeValue( "style", "background: red;"); continue }
{20MB -lt $_.Value } { $_.Parent.SetAttributeValue( "style", "background: orange;"); continue }
{10MB -lt $_.Value } { $_.Parent.SetAttributeValue( "style", "background: yellow;"); continue }
}
# Save the html out to a file
$xml.Save("c:\procs2.html")
# Open the thing in your browser to see what we've wrought
ii c:\procs2.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.