导航到网页后,PowerShell IE9 ComObject具有所有null属性 [英] PowerShell IE9 ComObject has all null properties after navigating to webpage

查看:66
本文介绍了导航到网页后,PowerShell IE9 ComObject具有所有null属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,该脚本可以导航到我们Intranet上的一个(大概)经典ASP页,以停止在该服务器上运行的Windows Service,这是该服务的部署过程的一部分(并在部署新文件后重新启动它).直到我们最近升级到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:\Projects\ABC\Scripts\Deploy.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属性在我的情况下为null),对于后一种情况,IE9默认为Intranet站点的兼容模式.我保存了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区域中且处于受保护模式下,那么您提交到受信任区域或Intranet或任何其他区域中的页面时,似乎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(以admin身份运行),这也不是问题

This is also a non-issue if you run your powershell in elevated mode (run as admin)

这篇关于导航到网页后,PowerShell IE9 ComObject具有所有null属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆