导航到网页后,PowerShell IE9 ComObject 具有所有空属性
我有一个 PowerShell 脚本,它导航到我们 Intranet 上的(大概)经典 ASP 页面,以停止在我们的服务器上运行的 Windows 服务,作为该服务部署过程的一部分(并在部署新文件后重新启动它).它运行良好,直到我们最近升级到 IE9.这是脚本.
I have a PowerShell script that navigates to a (presumably) classic ASP page on our intranet to stop a Windows Service running on our server as part of the deployment process for that service (and restarts it after deploying the new files). It ran fine until we recently upgraded to IE9. Here's the script.
# Open service page in IE
$ie = new-object -comobject InternetExplorer.Application
$ie.visible = $true
$ie.navigate($serviceUrl)
while($ie.busy) { start-sleep 1 }
# Stop service
$ie.Document.getElementById("dropDownActionList").value = "Stop"
$ie.Document.getElementById("buttonTakeAction").click()
while($ie.busy) { start-sleep 1 }
现在,当我运行脚本时,它成功启动了 IE,但抛出了以下错误:
Now when I run the script, it successfully launches IE, but throws the following error:
You cannot call a method on a null-valued expression.
At C:ProjectsABCScriptsDeploy.ps1:85 char:28
+ $ie.Document.getElementById <<<< ("dropDownActionList").value = "Stop"
+ CategoryInfo : InvalidOperation: (getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
当我在 PowerShell 中调查时,我发现如果我创建 IE ComObject,它起初具有有效的属性,但是一旦我导航到服务控制页面,所有属性都为空(几乎就像 ComObject走了?).例如,之前 HWND
属性具有有效值,但现在它为 null($ie.hwnd -eq $null
返回 true).当我导航到该页面时,PowerShell 中没有显示任何错误.
When I investigate in PowerShell, I find that if I create the IE ComObject, it at first has valid properties, but as soon as I navigate to the the service control page, all the properties are null (almost as if the ComObject gone away?). For example, before the HWND
property had a valid value, but now it's null ($ie.hwnd -eq $null
returns true). No error is displayed in PowerShell when I navigate to the page.
我看了一些类似的 问题,但第一个与我的情况不符(Document
属性在我的情况下为空),对于后一种,IE9 默认为内网站点的兼容模式.我保存了 ASP 页面并通过 w3c 验证器 运行它并且它抛出了一些错误(尽管没有与我正在尝试处理的元素).不幸的是,我无法修复这些.其他网站好像没有这个问题.对可能是什么问题的任何怀疑以及关于变通方法的建议?
I looked at some similar questions, but the first one doesn't match my circumstance (the Document
property is null in my case) and as for the latter one, IE9 defaults to compatibility mode for intranet sites. I saved the ASP page and ran it through the w3c validator and it threw some errors (although none related to the elements I'm trying to deal with). Unfortunately I can't fix those. Other sites don't seem to have this problem. Any suspicions on what the problem may be and recommendations on work-arounds?
我刚刚解决了这个......有点.在我关闭 IE 中的保护模式之前,我看到了相同的行为.这似乎与从一个安全区域提交到下一个安全区域有关.因此.. 假设您的原始页面位于 Internet 区域中,并且启用了保护模式,您提交到受信任区域或内联网或其他任何内容中的页面,则 COM 上下文似乎丢失了.大概是故意的.我将尝试修复区域,并保持保护模式.
I just worked through this.. sort of. I was seeing the same behavior until I turned off protected mode in IE. This seems to have something to do with submitting from one security zone to the next. So.. assuming that your original page is in the internet zone, with protected mode on, you submit to a page in a trusted zone or intranet or whatever, it seems like the COM context is lost. Probably intentional. I'm going to try fixing the zones, and keeping protected mode on.
希望这会有所帮助.
如果您在提升模式下运行 powershell(以管理员身份运行),这也不是问题
This is also a non-issue if you run your powershell in elevated mode (run as admin)