InternetExplorer.Application com对象和PowerShell中的Windows 2012 [英] InternetExplorer.Application com object and windows 2012 in powershell
问题描述
我试图访问的Internet Explorer浏览器COM对象与Windows 2012的文档。代码在Windows 2008中工作伟大的,但一旦我尝试在Windows 2012上运行它(新安装,尝试在多个服务器) ,同样的代码停止工作。换句话说,$ ie.document.documentHtml返回为null。
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.
以下是代码:
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("http://www.example.com/")
while($ie.busy) {start-sleep 1}
$ie.document.documentHtml.innerhtml
在windows 2012中更改了interexplorer com对象?如果是,我如何在Windows 2012中检索文档内容?
Has the interexplorer com object changed in windows 2012? and if yes, how do I do I retrieve the document contents in windows 2012?
提前感谢
编辑:添加一个赏金,以增甜。 Invoke-WebRequest是很好,但它只适用于Windows 2012,但我需要使用Internet Explorer,并在Windows 2008和Windows 2012上工作。我已经在某个地方安装微软Office解决了这个问题。它也不是一个选择。
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:由于我需要在多个Windows服务器上远程调用脚本(2008和2012),我不想手动复制文件
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
推荐答案
It's a know bug: http://connect.microsoft.com/PowerShell/feedback/details/764756/powershell-v3-internetexplorer-application-issue
解决方法的提取:
因此,这里有一个解决方法:
So, here's a workaround:
- 从安装它的位置复制
Microsoft.html.dll
(例如:从C:\Program Files(x86)\ Microsoft .NET \Primary Interop程序集到脚本位置(可以是网络驱动器) - 使用
Load-Assembly.ps1
脚本(下面提供的代码和 http://sdrv.ms/U6j7Wn )在内存中加载程序集类型
例如:.\Load-Assembly.ps1 -Path .\microsoft.mshtml.dll
- 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) - 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
然后照常操作创建IE对象等。警告:当处理write()和writeln()方法时,使用向下兼容的方法:IHTMLDocument2_write()和IHTMLDocument2_writeln()。
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().
这篇关于InternetExplorer.Application com对象和PowerShell中的Windows 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!