Link to home
Start Free TrialLog in
Avatar of Alex Young
Alex YoungFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell Hyperlinks

Hi,

I'm trying to embed a hyperlink in a html table this is the line of code.

$result | Add-Member -MemberType NoteProperty -Name 'More Detail' -Value "<a href='$comp.html'>$comp</a>" | ConvertTo-HTML -Fragment

and the formatting is

$a = "<style>"
$a = $a + "BODY{background-color:white;
               font-family:Tahoma;
               font-size:7pt;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding:0px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding:0px;border-style: solid;border-color: black;background-color:palegoldenrod;a:link.navlink { color:#0000cc }"
$a = $a + "</style>"
$a = $a + "</FONT>"

But the hyperlink is not working i'm just seeing the following


More Detail

 
<a href='Alex-PC.html'>Alex-PC</a>


any advice on getting the hyperlink working
Avatar of footech
footech
Flag of United States of America image

I think I need more detail.  I don't see where $a is relevant to the question.  What are you wanting to see from the link?
Avatar of Alex Young

ASKER

Thought you might need to see the formatting incase i'm missing something, I have got all the data for computers coming in a table but one of the colums is a hyperlink which is generated by another script to collect the software on that particular computer.

So when it runs you should be able to click on the link on the hardware inventory and it will link to that relevent $comp variable.html, I'm now rethinking it but not sure how to get the data in SQL so learning that - Would you suggest that putting the data into a SQL DB be easier to pull out these kind of reports?
Maybe the issue is the use of single quotes.  You might try
$result | Add-Member -MemberType NoteProperty -Name 'More Detail' -Value "<a href=""$comp.html"">$comp</a>" | ConvertTo-HTML -Fragment

I hardly ever use SQL I wouldn't be the best to advise you.
OK, I see what is happening.  ConvertTo-HTML is treating the <, >, and " characters as literal.  Found a webpage that described this at http://jdhitsolutions.com/blog/2012/04/create-an-html-powershell-help-page/
On the page it just mentioned < and > characters, but in my testing " was also converted.  So I modified his function.  You will be able to pipe HTML to it fix the characters as shown on the last line.
Function Convert-HTMLEscape {
 <#
 Convert &lt; and &gt; to < and >
 It is assumed that these will be in pairs
 Also convert $quot; to "
 #>
[cmdletbinding()]
Param (
 [Parameter(Position=0,ValueFromPipeline=$True)]
 [string[]]$Text
 )
  Process
  {
    foreach ($item in $Text)
    {
      if ($item -match "&lt;")
      {
        (($item.Replace("&lt;","<")).Replace("&gt;",">")).Replace("&quot;",'"')
      }
      else
      {
        #otherwise just write the line to the pipeline
        $item
      }
    }
  } #close process
} #close function

$result | Add-Member -MemberType NoteProperty -Name 'More Detail' -Value "<a href=""$comp.html"">$comp</a>" | ConvertTo-HTML -Fragment | Convert-HTMLEscape

Open in new window

Hi, Thanks for that but still same issue for some reason it's picking up the variable so changes that to the correct computers but it's displaying the following

<a href="Alex-PC.html">Alex-PC</a>

It's so random if i take away the css formatting it all works i'm just wondering if something is wrong with that line of code.

Would it help if i show the whole code?
Perhaps.  I think the HTML would be more helpful (at least a couple lines relevant to the link).
I've attached - it's got some of me testing but see if you can figure it out its driving me mad lol something so simple :)
Full-HardwareSoftware---Copy.txt
Thanks.  How about the HTML source (at least part of it that is relevant)?
ah sorry, here it is the line for the hyperlink

<td>&lt;a href=&quot;Alex-PC.html&quot;&gt;Alex-PC&lt;/a&gt;</td></tr>
</table>
</body></html>
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant!!! Solved It thank you so much knew it be something simple :) this is the working code

$results | ConvertTo-Html -Title "PC Inventory" -head $PCInvFormattingPage -Body $PCInvFormatting | Convert-HTMLEscape | Out-File $htmlFile