Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, January 3, 2014 8:29 PM
How do I include HTML tags in the table output generated by the ConvertTo-HTML command?
For example, if I had an array of objects like this:
FriendlyName,URL
Microsoft,www.microsoft.com
Google,www.google.com
Yahoo,www.yahoo.com
How could I make my ConvertTo-Html output something like this:
<a href="www.microsoft.com">Microsoft</a>
<a href="www.google.com">Google</a>
<a href="www.yahoo.com">Yahoo</a>
I have already tried making another property on my objects (OutHTML) with a string of "<a href=" + [char]34 + $URL + [char]34 + ">" + $FriendlyName + "</a>"
Unfortunately, ConvertTo-Html does its job too well and it converts all of my HTML tags to use < and > so that the < and > will display on the webpage! Thanks for your help!
All replies (3)
Monday, January 6, 2014 8:16 AM âś…Answered
Hi NamedJason,
In addition, To output to a html file as text string, this script may be helpful for you:
How to render clickable link using Powershell
I hope this helps.
Friday, January 3, 2014 9:20 PM
You could pipe 'something' into the convertto-html command and leave your 'real' content as part of a -PostContent argument, which will leave the formatting intact:
You can do something like:
$URLList = @()
<# Your Code Loop Here#> {
$URLList += "<TR><TD><a href=`"$URL`">$FriendlyName</a></TD></TR>"
}
ConvertTo-HTML -Body "URL List:" -PostContent "<Table>$URLList</Table>"
There's a lot more you can do with ConvertTo-HTML, like add a -Head argument with css styles, a -PreContent section including HTML tags, and more:
http://technet.microsoft.com/en-us/library/hh849944.aspx
Hope this helps! What I'm proposing is perhaps just a cheat, but it will work.
Edit: I added the extra HTML tags as an example of what to do. Once you bypass the HTML formatting you need to add it yourself.
Monday, January 6, 2014 5:28 PM
Thank you, that looks like it does exactly what I need! Can you tell me if I'm correctly understanding what's going on in their example?
$pso = New-Object PSObject -Property @{
SiteUrl = $spweb.url
PageTitle= $PageTitle
PageUrl = "<a href='$PagesUrl'>$PageTitle</a>"
} | ConvertTo-Html
Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($pso) |
Out-File \\myfolder\InventoryReports\$CurrentMonth.html
At first, they generate the powershell object with the desired properties (including a property for the formatted hypertext output), which is then converted in place to HTML. It then loads the .Net framework System.Web class so that they can access the HtmlDecode() method. They pass the HTML'ified $pso to that method, which searches through $pso for any HTML special characters (the >, <, etc.) and replaces them with the actual characters, then writes the modified string out to a file. Thanks for your help!