Powershell 网页自动化适用于 Internet,而不是 Intranet [英] Powershell Web Page Automation works on Internet, not Intranet

查看:20
本文介绍了Powershell 网页自动化适用于 Internet,而不是 Intranet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Powershell 进行一些简单的自动化,从我们公司的本地 Intranet 页面之一中提取链接 URL,然后对这些 URL 进行一些处理.最后,我将使用脚本打开每个链接并单击页面上的按钮.我在 Windows 7 x64 中使用 Internet Explorer 9.

I'm trying to do some simple automation with Powershell, pulling link URLs from one of our company's local intranet pages, and then doing some work with those URLs. Eventually I'll use the script to open each link and click a button on the page. I'm using Internet Explorer 9 in Windows 7 x64.

下面是一个简单的 powershell 脚本示例,它显示页面上的所有链接:

Here's an example of a simple working powershell script that displays all the links on a page:

$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate( "http://www.reddit.com" )
While ($ie.Busy) {
    Sleep 1
}

$links = $ie.Document.getElementsByTagName("a")
$links | foreach {
    write-host $_.href
}

此脚本工作正常,直到我将 URL 替换为本地 Intranet 站点.它遵循正常的 URL 方案( http://internaldomain.com/etc ),但它被识别为内部网站.一旦我尝试抓取 Intranet 区域中的页面,$ie.Document 值突然变为 NULL 并且脚本失败.

This script works fine until I replace the URL with a local intranet site. It follows the normal URL scheme ( http://internaldomain.com/etc ), but it's recognized as an intranet site. Once I'm trying to scrape a page in the intranet zone, the $ie.Document value suddenly becomes NULL and the script fails.

我猜这与该区域的一些模糊设置有关...我不确定.我在网上找到了一些建议,例如将其添加到您信任的站点,但这并没有奏效.这是我第一次使用 Powershell 进行网络自动化,因此我们将不胜感激.

I'm guessing it's related to some obscure setting for that zone... I'm not sure. I found some suggestions online such as adding it to your trusted sites, but that has not worked. This is my first time using Powershell for web automation, so any help or insight would be appreciated.

推荐答案

也许解决方案在这里:http://blogs.msdn.com/b/ieinternals/archive/2011/08/03/internet-explorer-automation-protected-mode-lcie-default-integrity-level-medium.aspx

它解释了不同级别的选项卡,在 ie.您必须使用媒体选项卡"在本地区域中导航.

It explained the different levels of tabs, in ie. You have to use the "medium tab" to navigate in local zone.

基本上,保持 ie 设置并使用脚本的最佳方法是创建一个注册表项,如上面链接中所述.

Basically, the best way to keep your ie settings and use your script is to create a registry key, as explained in the link above.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTInternetExplorer.ApplicationMedium]

[HKEY_CLASSES_ROOTInternetExplorer.ApplicationMediumCLSID] 
@="{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}"

在你的脚本中,使用这个新的 com 对象:

And in your script, use this new com object:

$ie = new-object -Com InternetExplorer.ApplicationMedium
...

这篇关于Powershell 网页自动化适用于 Internet,而不是 Intranet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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