Question

I am using below powershell script to write the output to a html file, the issue is its rendering everything as text.

My code:

$PagesObject = New-Object PSObject
Add-Member -input $PagesObject noteproperty 'Site Url' $spweb.url
Add-Member -input $PagesObject noteproperty 'Page Title' $PageTitle
Add-Member -input $PagesObject noteproperty 'Page Url' "<a href="$PagesUrl">$PageTitle</a>"     
$results += $PagesObject
$results | ConvertTo-HTML -head  $a  -body | Out-File \\myfolder\InventoryReports\$CurrentMonth.html

I want to render it as clickable link

check below screenshot to see how it is rendering now enter image description here

Thanks in advance

Était-ce utile?

La solution

PowerShell encodes special characters. One way to solve this would be to convert them back and then write the result ot a file:

$pso = New-Object PSObject -Property @{
    SiteUrl = $spweb.url
    PageTitle= $PageTitle
    PageUrl = "<a href='$PagesUrl'>$PageTitle</a>"  
} | ConvertTo-Html


$pso -replace '&gt;','>' -replace '&lt;','<' -replace '&#39;',"'" | 
Out-File \\myfolder\InventoryReports\$CurrentMonth.html

UPDATE:

Here's a better way that will take care of all html entities:

Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($pso)

Autres conseils

i.e.

$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

There is a more general way to do this, other than using Html::Decode or RegExps. It is far more complicated, but it can be usefull, when some columns, besides link columns, contain special HTML characters, that shouldn't be processed like links (e.g. $PageTitle have something like "foo<bar", what will break markup after using Html::Decode).

This approach using the fact, that ConvertTo-Html produces valid XHTML, which can be used in Powershell/.Net XML facilities. The main idea is to convert cmdlet output to XML document, then retrieve link columns with XPath and modify them to create links using DOM functions.

Here is code snippet:

$pso = New-Object PSObject -Property @{
    SiteUrl = 'foobar.org'
    PageTitle = 'x&z a<b<c'
    PageUrl = 'foobar.org/contact.html'
} | ConvertTo-Html

$xmlDoc = [xml]$pso
$xhtmlNS = 'http://www.w3.org/1999/xhtml'
$xmlNSManager = New-Object System.Xml.XmlNamespaceManager $xmlDoc.NameTable
$xmlNSManager.AddNamespace('x', $xhtmlNS)
$xmlDoc.SelectNodes('//x:table/x:tr/x:td[3]', $xmlNSManager) | %{
    $a = $xmlDoc.CreateElement('a', $xhtmlNS)
    $a.setAttribute('href', $_.'#text')
    [void]$a.AppendChild($xmlDoc.CreateTextNode('Contact Us'))
    [void]$_.ReplaceChild($a, $_.FirstChild)
}

$xmlDoc.OuterXml | Out-File 'test.html'

XPath string to retrieve n-th column cells is '//x:table/x:tr/x:td[n]', where n - is number of column (index starts at 1). Some clarifications about using XML namespaces (e.g. XmlNamespaceManager and funky 'x:' prefix) can be found here: Xml Namespace breaking my xpath!

To modify several columns at once, following XPath can be used: '//x:table/x:tr/x:td[position()=1 or position()=2 or ... or position()=last()]'

foreach ($td in $xmlDoc.SelectNodes('//x:table/x:tr/x:td[position()=1 or position()=2 or ... or position()=last()]', $xmlNSManager)) {
    # processing first column cells
    [void]$foreach.MoveNext()
    # processing second column cells
    [void]$foreach.MoveNext()
    # etc
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top