Unable to use IHTMLDocument2
Asked Answered
L

4

7
$wc = New-Object System.Net.WebClient
$DownloadString = $wc.DownloadString("http://www.example.com")
$HTML = New-Object -ComObject "HTMLFile"
$HTML.IHTMLDocument2_write($DownloadString)

Server script runs on

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14409  1005

Development PC

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  502

My Windows 10 development PC worked fine with the code above. I want to run this on my Server 2008 R2 x64 machine. I upgraded it to PowerShell v5. I get the following:

Method invocation failed because [System.__ComObject] does not contain a method named 'IHTMLDocument2_write'.

And later down the line...

Unable to find type [mshtml.HTMLDocumentClass].
Lebbie answered 19/9, 2017 at 19:0 Comment(1)
You can check the list of methods in $HTML with $HTML | Get-Member, as explained in this documentation.Supportive
P
25

My friend and I were trying to nail down this issue. We kept getting this error on his machine while the same script worked on mine, (same Windows 10 build version on both).

Method invocation failed because [System.__ComObject] does not contain a method named 'IHTMLDocument2_write'.
At <script_path\script_name>:184 char:1
+ $HTML.IHTMLDocument2_write($content)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (IHTMLDocument2_write:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

We found that on his computer the $html object had ie9 properties and on mine it had IHTML properties. He identified that my computer had MS Office installed, but his did not. He tried the code on another of his computers running Server 2008 that did have Office (even an old version like 2010) installed and our script worked fine.

Proposed solution:

$HTML = New-Object -Com "HTMLFile"

try {
    # This works in PowerShell with Office installed
    $html.IHTMLDocument2_write($content)
}
catch {
    # This works when Office is not installed    
    $src = [System.Text.Encoding]::Unicode.GetBytes($content)
    $html.write($src)
}
Pediatrician answered 19/2, 2018 at 5:6 Comment(2)
Unlike the accepted answer by @TylerMontney, this works when Office is not installed as well as in pwsh (PSVersion 6.2.0, PSEdition Core)Rhombencephalon
Excellent, this was a difficult one to crack.Channel
L
2

Updated:

Ironic, how I ran into a similar issue years later, came across this post and went "hey I asked this question".

l0wm3mory's answer is now the correct answer.

Original:

Question that helped: Can't use InternetExplorer.Application object?

Copied Microsoft.mshtml.dll from my machine that worked to the server in C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies. Then added Add-Type -Path "C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\microsoft.mshtml.dll" at the beginning of my script.

I also noticed some IE security boxes appear (when running my script) and it's possible the IE security settings of a Windows server would interfere (being it's much higher than a client would be). Perhaps if my settings were lowered, this would be resolved without copying the .dll. However, I think upgrading to PSv5 was crucial (as even enum wasn't recognized).

Lebbie answered 19/9, 2017 at 20:39 Comment(0)
M
0

I dont't know why,but after installed microsoft office on the computer,everything works fine for me. Tried on two different computer.

Massarelli answered 15/3, 2019 at 12:52 Comment(0)
V
0

I have the problem of the same above and was able to fix it by downloading the dll file from the internet and register it in Powershell. I found the dll at https://www.dllme.com/dll/files/microsoft_mshtml_dll.html by a search but not sure it is official or not. Then I copy it to System32 folder and run the two command lets below.

Add-Type -Path "C:\Windows\System32\Microsoft.mshtml.dll"
Add-Type -AssemblyName "Microsoft.mshtml"

And voice la, the problem gone.

Varityper answered 3/3, 2020 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.