InternetExplorer.Application com object and windows 2012 in powershell

Youn Elan picture Youn Elan · Jan 17, 2014 · Viewed 7.6k times · Source

I am trying to access the document of an internet explorer com object with windows 2012. The code works great in windows 2008 but as soon as I try to run it on windows 2012 (fresh install, tried on more than one server), the same code stops working. In other words, $ie.document.documentHtml returns as null.

Below is the code:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("http://www.example.com/") 
while($ie.busy) {start-sleep 1}
$ie.document.documentHtml.innerhtml

Has the interexplorer com object changed in windows 2012? and if yes, how do I do I retrieve the document contents in windows 2012?

Thanks in advance

edit: Added a bounty to sweeten things up. Invoke-WebRequest is nice but it works only on windows 2012 but I need to use internet explorer and have it work both on windows 2008 and windows 2012. I have read somewhere that installing microsoft office solves the issue. It is not an option either.

edit2: as I need to remotely invoke the script on multiple windows server (both 2008 and 2012), I would prefer not to copy files manually

Answer

CB. picture CB. · Jan 22, 2014

It's a know bug: http://connect.microsoft.com/PowerShell/feedback/details/764756/powershell-v3-internetexplorer-application-issue

An extract from the workaround:

So, here's a workaround:

  1. Copy Microsoft.html.dll from a location where it is installed (eg: from C:\Program Files(x86)\Microsoft.NET\Primary Interop Assemblies to your script's location (can be a network drive)
  2. Use the Load-Assembly.ps1 script (code provided below and at: http://sdrv.ms/U6j7Wn) to load the assembly types in memory eg: .\Load-Assembly.ps1 -Path .\microsoft.mshtml.dll

Then proceed as usual to create the IE object etc. Warning: when dealing with the write() and writeln() methods use the backward compatible methods: IHTMLDocument2_write() and IHTMLDocument2_writeln().