Kenny
Kenny

Reputation: 151

Powershell -replace <td> with a special character of ">" not working

I've got a block of code that does -replace on a number of <td> entries in some HTML output.

The data that is read comes from a .csv, so all values coming in are string type

The entire block of code is as follows:

$Report | Select Server, WMI, SVC, UptimeDays | ConvertTo-HTML | ForEach {

$PSitem -replace "<td>Not installed</td>", "<td style='background-
color:Yellow;color=black'>Not installed</td>"`

-replace "<td>Failed</td>", "<td style='background-
color:#FF0000;color=white'>Failed</td>"`

-replace "<td>Installed/Not running</td>", "<td style='background-
color:#FF0000;color=white'>Installed/Not running</td>"`

-replace "<td>WMI timed-out</td>", "<td style='background-
color:#FF0000;color=white'>WMI timed-out</td>"`

-replace "<td>Offline</td>", "<td style='background-
color:#FF0000;color=white'>Offline</td>"`

-replace "<td>Uptime > 7 days</td>", "<td style='background-
color:Yellow;color=black'>Uptime > 7 days</td>"

} | out-String

The -replace on all entries, with the expected colored background/text, but not for the "<td> Uptime > 7 days</td>"

...I'm assuming it's because I've got the ">" in there. I would rather not re-format the input CSV to remove the ">". Is it just a matter of adding an escape "/" character in the right place? How would I do that with the above code?

Upvotes: 0

Views: 578

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

Even though you didn't answer the relevant part of my comment, I'm pretty certain that's it:

Test data:

PS D:\t> 1,2,3|select @{n='num';e={$_}},@{n='days';e={'days > 7'}}

num days    
--- ----    
  1 days > 7
  2 days > 7
  3 days > 7

Converted to HTML:

PS D:\t> 1,2,3|select @{n='num';e={$_}},@{n='days';e={'days > 7'}} | ConvertTo-Html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/></colgroup>
<tr><th>num</th><th>days</th></tr>
<tr><td>1</td><td>days &gt; 7</td></tr>
<tr><td>2</td><td>days &gt; 7</td></tr>
<tr><td>3</td><td>days &gt; 7</td></tr>
</table>
</body></html>

See the > has been replaced with the HTML entity code for the greater-than symbol, &gt;.

So your replace will have to match "<td>Uptime &gt; 7 days</td>".

Upvotes: 2

Related Questions