Answered by:
ConvertTo-HTML question

Question
-
Hi
I was pulling a list of inactive computers from AD and sending them as an email monthly. Last week I added a link (helpdesk.ourdomain.com) to the email which takes the user to directly to the asset in our inventory. What I didn't take in to consideration is that ConvertTo-HTML would convert < and > to < and >
Same issue posted here https://stackoverflow.com/questions/23143393/powershell-convertto-html-is-translating-and-symbols-to-lt-and-gt-how
I can see that the solution is to use regular expressions with Replace operator. Is there any other way to solve this problem?
$var = Get-ADComputer -Filter {LastLogonTimeStamp -lt 30} -Properties Name,OperatingSystem , LastLogonTimeStamp, Created, Description | Select @{Name="Name"; expression={"<a href='https://helpdesk.ourdomain.com/SystemList.jsp?k&searchField="+$_.Name+"'>"+$_.Name+"</a>"}}, Description, OperatingSystem , Created, @{N='LastLogonTimeStamp'; E={[DateTime]::FromFileTime($_.lastlogontimestamp)}} | ConvertTo-HTML | Out-String
essentially i need "<" replaced with "<" , ">" with ">" and "'" with "'"
Thanks
- Edited by net_tech Friday, August 30, 2019 2:42 AM
Thursday, August 29, 2019 11:32 PM
Answers
-
Using XML is more powerful and more specific.
$tmplt = 'https://helpdesk.ourdomain.com/SystemList.jsp?k&searchField={0}' $css = @' <style> th { color:red; background-color: lightblue; } table {border-collapse: collapse} table, th, td { border: 1px solid black; } </style> '@ $date = [datetime]::Today.AddDays(-30) $properties = 'Name', 'OperatingSystem', 'LastLogonDate', 'Created', 'Description' [xml]$xml = Get-ADComputer -Filter {LastLogonDate -lt $date} -Properties $properties | Select-Object $properties | ConvertTo-HTML -Head $css $xml.html.body.table.tr | Where-Object{-not $_.th} | ForEach-Object{ $node = $_.ChildNodes[0] $name = $node.'#text' $node.InnerText = '' $url = $tmplt -f $name $el = $xml.CreateElement('a') $el.InnerText = $name $el.SetAttribute('href',$url) [void]$_.ChildNodes[0].AppendChild($el) } $xml.Save("$pwd\test.htm") . "$pwd\test.htm"
\_(ツ)_/
- Marked as answer by net_tech Saturday, August 31, 2019 3:23 PM
Saturday, August 31, 2019 2:14 PM
All replies
-
Use the "=replace" operator to replace the characters.
\_(ツ)_/
- Proposed as answer by BOfH-666 Friday, August 30, 2019 6:33 AM
Friday, August 30, 2019 3:58 AM -
Saturday, August 31, 2019 1:14 PM
-
found my answer here
...Out-String | % {$_.replace("<", "<").replace(">", ">").replace("'", "'" ) }
Saturday, August 31, 2019 1:26 PM -
Using XML is more powerful and more specific.
$tmplt = 'https://helpdesk.ourdomain.com/SystemList.jsp?k&searchField={0}' $css = @' <style> th { color:red; background-color: lightblue; } table {border-collapse: collapse} table, th, td { border: 1px solid black; } </style> '@ $date = [datetime]::Today.AddDays(-30) $properties = 'Name', 'OperatingSystem', 'LastLogonDate', 'Created', 'Description' [xml]$xml = Get-ADComputer -Filter {LastLogonDate -lt $date} -Properties $properties | Select-Object $properties | ConvertTo-HTML -Head $css $xml.html.body.table.tr | Where-Object{-not $_.th} | ForEach-Object{ $node = $_.ChildNodes[0] $name = $node.'#text' $node.InnerText = '' $url = $tmplt -f $name $el = $xml.CreateElement('a') $el.InnerText = $name $el.SetAttribute('href',$url) [void]$_.ChildNodes[0].AppendChild($el) } $xml.Save("$pwd\test.htm") . "$pwd\test.htm"
\_(ツ)_/
- Marked as answer by net_tech Saturday, August 31, 2019 3:23 PM
Saturday, August 31, 2019 2:14 PM -
jrv,
Can't agree more, your approach with XML is way better! Thank you for sharing
Saturday, August 31, 2019 3:26 PM