Powershell –replace method and my own frustration.

Summary:
I added some code to my VMWare Infrastructure script for a new column of data to show a tooltip when clicked or hovered over.  Because I also have it export a csv file from this code, I use the –replace method to strip the html formatting.  Because of one section, more data than I wanted was being stripped.  Why?  Probably because I used this: .*>
Resolution:
Anyway here is the string I was using:
   1: foreach {$_ -replace("<p style='font-weight:bold;color:orange'>","") -replace("<p style='font-weight:bold;color:red'>","") -replace("<a class='cluster' href=./healthcheck.htm#.*> ","") -replace("</p>","") -replace ("</a>","") -replace ("<a href='#' class='tt'>","") -replace ("<span class='tooltip'><span class='top'></span><span class='middle'>Digi says... <br />.*","") -replace ("</span><span class='bottom'></span></span>","") }|

And here is how I fixed it:


   1: foreach {$_ -replace("<p style='font-weight:bold;color:orange'>","") -replace("<p style='font-weight:bold;color:red'>","") -replace("</p>","") -replace ("</a>","") -replace ("<a href='#' class='tt'>","") -replace ("<span class='tooltip'><span class='top'></span><span class='middle'>Digi says... <br />.*","") -replace ("</span><span class='bottom'></span></span>","") -replace("<a class='cluster' href=./healthcheck.htm#.*> ","")}

Simply by moving one replace statement w/ the .*> to the end of the chain solved my problem.  I still don’t quite understand, but whatever, some other time and place.

Side note:

I think I’m going to make separate properties in my report object for RAW data, add html formatted versions to other properties, and use Select statements for export to avoid this conundrum altogether. 

Comments

Popular posts from this blog

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

MacOS: AnyConnect VPN client was unable to successfully verify the IP forwarding table modifications.

vCenter: Cluster Skip Quickstart Workflow via API