PowerShell hang using innerHTML

Moray Henderson (INT) 1 Reputation point
2022-11-03T15:14:59.377+00:00

Did something happen in PowerShell or .NET mshtml last Tuesday? I have a PowerShell script which has been successfully processing HTML for many years. It takes an existing HTML document, finds a particular element and updates it. Effectively it does something like this:

function Build-HTML {  
    '<h1>New for Today</h1>'  
    '<p><br/></p>'  
    '<h1>Index by Message</h1>'  
}  
$content = Get-Content $PSScriptRoot\original.html | Out-String  
$html = New-Object -ComObject HTMLFile  
$html.IHTMLDocument2_write($content)  
$newcontent = Build-HTML | Out-String  
$html.GetElementById('DynamicContent').innerHtml = $newcontent  

Last Tuesday it stopped working: PowerShell simply hung on that last line setting innerHtml. Nothing else had changed. After noticing that it worked with a here string, I did eventually find a fix:

$newcontent = (Build-HTML | Out-String).Trim()  

Has something changed in the way PowerShell returns object[] from functions? Or in how Out-String processes such arrays? Or in the innerHtml setter itself, making it hang if there is an extra blank line on the end of the text? But it seems like a new bug has been introduced somewhere.

Kind regards,

Moray.

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-11-03T18:26:32.357+00:00

    There are multiple versions of Powershell and we have no idea what changed on your machine last Tuesday. If your code suddenly starts doing something different then the first thing you should do is identify what has changed. For example if you applied Windows updates then go to the update history and see what changes were made. Did any of them impact the version of Powershell that you're using? If so then revert the update and see if the problem goes away.

    If there were no updates then the problem is somewhere else. Where that problem is would be hard to diagnose. At that point you might need to step through the script using VS Code or similar to see what is going on. It could be the HTML that you're reading has changed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.