Powershell: replace values in html


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

Values are:


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


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


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">
          <th scope="col"><em># Servers</em></th>
       <TR class="KPIServers">
          <TD align=center>112</TD>

Do not see what is going wrong.
Please advise.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", 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

janhoedtAuthor Commented:
Sorry, does not work.
Qlemo"Batchelor", 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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 :-)
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
No output? You should have just received the unchanged file content on screen.
janhoedtAuthor Commented:
Great, works as a charm, thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.