Share via


adding a (href) hyperlink to html(convertTo-HTML) via powershell

Question

Friday, May 2, 2014 7:20 AM

  Im trying to add a href hyperlink to my HTML as below as im sending it as an email.

But it's not working for me.

Has anyone experienced this? How can I get this rolling?

I have already added

$message.IsBodyHTML = $true to convert to html

    $message.Body = $exportlist | Select-Object Sponsor,Activate,Category,FileName,@{name="Link"; expression={"<a href=""" + $web.Url + "/" + $_.Sponsor + "/Activate/" + $_.FileName + """>Link</a>"}} | ConvertTo-Html -Head $a        

Thanks in Advance

All replies (6)

Monday, May 5, 2014 3:31 AM âś…Answered

Thanks Guys.
Using System.Web.HttpUtility]::HtmlDecode did the trick.
Cheers


Friday, May 2, 2014 8:00 AM

It's always easiest to diagnose when you include the results as well.

Try reeding in the link as a plain URL. The code will probably identify that and wrap it in a URL which you could correct afterwards. I doubt it will work nicely with a pre-converted HTML line.

Failing that, try converting your table to HTML using the -fragment switch, then you can splice it into the message in the same way you're adding in the header. That will only generate the table for your data, which would make appending your link much easier.


Friday, May 2, 2014 8:16 AM

Thanks Alex the best result is like so.This is what i can see in my email

<a href="http://xxx/">Link</a>

As you can see i need this converted to Link

Cheers


Friday, May 2, 2014 8:22 AM

Hi,

Can you try to write it to a htm file and see the output? 

http://technet.microsoft.com/en-us/library/ff730936.aspx

something like below:

Get-Service | Select-Object Sponsor,Activate,Category,FileName,@{name="Link"; expression={"<a href=""" + $web.Url + "/" + $_.Sponsor + "/Activate/" + $_.FileName + """>Link</a>"}} | ConvertTo-Html -Head $a | Out-File C:\Scripts\Test.htm

Invoke-Expression C:\Scripts\Test.htm

Friday, May 2, 2014 8:25 AM

That's because it's double encoding the link. You're giving it a load of characters and it's putting them in as text. Exactly as the ConvertTo-HTML is supposed to.

Pass in the unmodified string and it'll probably output a link, albeit without the nicer center text. You can then clean that up post-creation if you really care.


Monday, May 5, 2014 12:57 AM

Thanks guys for the reply.

When i output it ad HTML its fine.

I can see the link as <a href=http://xxx.baba.com>Link</a> but when i view it in my outlook as email.
It doesn't change to a link just get the text. :(
Cheers