Powershell: replace values in html

Hi,

I would like to replace html tags in a website. So I have $ServersHtmlValueOriginal and $ServersHtmlValueNew (which I got via regex).

Values are:

$ServersHtmlValueOriginal

<TR class="KPIServers">
          <TD align=center>112</TD>
        </TR>

$ServersHtmlValueNew  

<TR class="KPIServers"><TD  align=center>118</TD></TR>


Bottomline:

Now, when I want to replace the values it does not work, nothing happens when executing this line:
$(Get-Content -Path $KpiDashBoardPath) -replace $ServersHtmlValueOriginal, $ServersHtmlValueNew # | Set-Content
the value should be 118 but stays 112




$(Get-Content -Path $KpiDashBoardPath) gives
 <td><div align="center">
      <table width="200" border="1">
        <tr>
          <th scope="col"><em># Servers</em></th>
          </tr>
       <TR class="KPIServers">
          <TD align=center>112</TD>
        </TR>
      </table>

Do not see what is going wrong.
Please advise.
J.
janhoedtAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"Does not work" is like "car does not drive". This is not helpful.

However, I have been wrong about <> having special meaning, I mixed that up with \<\>. So this has not been the issue.

I can see a potential issue: line breaks. Get-Content as you have used it reads in line after line, and so a multi-line replacement will not work. We can tell Get-Content to read the file as-is, into a single string, and then we are able to apply the replacement.
(Get-Content -Raw $KpiDashBoardPath).Replace($ServersHtmlValueOriginal, $ServersHtmlValueNew)
(Get-Content -Raw $KpiDashBoardPath) -replace $ServersHtmlValueOriginal, $ServersHtmlValueNew

Open in new window

are both valid.
1
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The -replace operator uses regular expressions, and <> have a special meaning. Use the string method .Replace instead:
(Get-Content -Path $KpiDashBoardPath).Replace($ServersHtmlValueOriginal, $ServersHtmlValueNew)

Open in new window

0
 
janhoedtAuthor Commented:
Sorry, does not work.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
janhoedtAuthor Commented:
Thanks! Will check asap. I know "does not work" isn't much of a description, but there was just no result at all/no output so no errors or other to report :-)
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
No output? You should have just received the unchanged file content on screen.
0
 
janhoedtAuthor Commented:
Great, works as a charm, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.