【问题标题】:How to prevent the Open/Save dialog when navigating with InternetExplorer.Application in powershell?在 Powershell 中使用 InternetExplorer.Application 导航时如何防止打开/保存对话框?
【发布时间】:2016-03-09 19:38:09
【问题描述】:

我正在尝试下载文件,但没有看到 Internet Explorer 的打开/保存对话框。登录表单比发送 Web 请求复杂得多,因此我仅限于使用 InternetExplorer.Application。

$credentials = Get-Credential -Message "Enter the username and password..."
$username = $credentials.UserName
$password = $credentials.GetNetworkCredential().password

$ie = New-Object -com InternetExplorer.Application 
$ie.navigate("https://example.com/getFile?downloadId=123")

$ie.document.getElementById("username").value= "$username" 
$ie.document.getElementById("password").value = "$password" 
$ie.document.getElementById("fm1").submit()

在执行上述代码时,登录工作完美,下载正在启动,但我看到一个像下面这样的窗口,这是我想要抑制的。虽然我知道最终用户可以更改此设置,但任何通过 powershell 自动下载的建议都会很好。

上面是一个示例窗口。

【问题讨论】:

标签: .net windows powershell com web-scraping


【解决方案1】:

正如@beatcracker 所指出的,通过注册表设置,您可以禁用文件类型的提示。

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\AttachmentExecute\{0002DF01-0000-0000-C000-000000000046} 键(如果需要,创建它)内,您必须为每种文件类型添加一个条目。

首先,您必须获取要定位的文件类型的 ProgID

可以使用 assoc 命令获取:

C:\Windows\System32>assoc .docx
.docx=Word.Document.12

然后,您必须在注册表中创建一个名为 Word.Document.12 的新值,其值为 0

当然,这需要为每个工作站的每个用户部署(GPO?)......这很乏味。

【讨论】:

  • 我已经为exe 文件(IE 10/ Server 2012,站点添加到Local Intranet 区域)尝试了这个,但没有运气。我还没有尝试过 cmets 文章中提到的EditFlags
  • 我在 Internet 网站上没有任何关于 exe 文件的提示...我一定已经搞砸了...
  • 您的 IE/OS 版本是多少?您可以在这里发布您的注册表项内容吗?
  • 不,我的意思是它已经以某种方式被禁用,我的注册表项只包含 PDF 文件的条目。
  • 哦,我明白了。顺便说一句,我认为它可以在没有 GPO 的情况下完成,只需在启动 IE 之前编写注册表并在关闭 IE 对象后恢复它。
【解决方案2】:

我已经以下面的脚本形式充实了@sodawillow 的答案,因为我将从不同的服务器/帐户运行网络爬虫脚本,但我无法得到离开的提示。 IE 版本为“Windows Server 2016”。希望其他人会有更多的运气。如果您确实使用我的脚本,请告诉我。

$path = "HKCU:\SOFTWARE\Microsoft\Windows\Shell\"
$item1 = "AttachmentExecute"
$item2 = "{0002DF01-0000-0000-C000-000000000046}"
$path1 = $path+$item1
$path2 = $path+$item1+"\"+$item2

if ($item1 -notin (Get-ChildItem $path | foreach-object {$_.name.split("\")[-1]})) 
{
    New-Item –Path $path –Name $item1
    New-Item –Path $path1 –Name $item2
    if ((Get-Item $path2).Property -notcontains "Excel.Sheet.12") 
    {
        New-ItemProperty -Path $path2 -Name "Excel.Sheet.12" -PropertyType "Binary"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2012-03-14
    • 2012-01-16
    • 1970-01-01
    • 2018-02-18
    • 2010-10-14
    相关资源
    最近更新 更多