使用PowerShell自动执行网站登录和文件下载 [英] Use PowerShell to automate website login and file download

查看:515
本文介绍了使用PowerShell自动执行网站登录和文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PowerShell自动登录网站并下载PDF文件.互联网上有大量示例显示了如何执行此操作(使用Invoke-WebRequestWebClientHttpWebRequestInternetExplorer.Application),但是大多数不需要先登录.有人通过登录显示它,但我无法让他们正常工作.我已经接近使用InternetExplorer.Application:

I want to use PowerShell to automate logging in to a website and downloading a PDF file. There are loads of examples on the internet that show how to do this (using Invoke-WebRequest, WebClient, HttpWebRequest, or InternetExplorer.Application), but most don't require a login first. Some show it with login, but I can't get them to work. I'm close with using InternetExplorer.Application:

$username = "xxxxx"
$password = "yyyyy"
$url = "https://example.com/login.aspx"
$usernameElementId = "aaaaa"
$passwordElementId = "bbbbb"
$submitButtonElementId = "ccccc"

$ie = New-Object -com InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate($url)

while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}

$ie.Document.getElementById($usernameControlId).value = $username
$ie.Document.getElementById($passwordControlId).value = $password
$ie.Document.getElementById($submitButtonElementId).click()

while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
Start-Sleep -m 2000

$url = "https://example.com/statements/201607.pdf"
$outFilePath = "C:\Downloads\Statement_201607.pdf"
$ie.Navigate($url)

while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}

# Script works up to this point--the pdf document is shown in IE.
#The file downloaded in the next step is empty.

$ie.Document.body | Out-File -FilePath $outFilePath

我的问题:如何在脚本的最后一步中下载PDF文档?

My question: how do I get the PDF document downloaded in the last step of the script?

我已经尝试使用WebClientInvoke-WebRequest来执行相同的任务,但是由于身份验证的原因,我一直遇到错误.我已经尝试过在登录后捕获cookie,并将它们与下一个请求一起传递,但是什么也没有.如果有人有使用其他方法执行此操作的可行示例,那么我无所不能.实际上,我更倾向于避免自动执行IE,但我将采用任何可行的解决方案.

I've tried to do this same task with WebClient and Invoke-WebRequest, but I keep getting errors, because of the authentication piece. I've tried capturing the cookies after login and passing them with the next request, but nothing. If someone has a working example of doing this using another means, I'm all ears. In fact my preference would be to avoid automating IE, if possible, but I'll take any working solution.

推荐答案

理想情况下,您可以使用如您所说的Invoke-WebRequest,但这实际上取决于网站的设置.如果只是在数据库中查询登录名并从中生成Cookie,则不可能(但仍然值得一试):

Ideally you would be able to use Invoke-WebRequest as you have said, however this really depends on how the website is set up. If it's just querying a database for the login and generating a cookie from that, it's likely not possible (but still worth a shot):

$url = "https://example.com/statements/201607.pdf"
$outFilePath = "C:\Downloads\Statement_201607.pdf"

# Prompt for password
Invoke-WebRequest -Uri $url -Credential MyUser -OutFile $outFilePath
# MyUser can be substituted with a credential object but it's complex, Google it

哎呀,请尝试完全不使用Credential参数的情况,再次取决于它可能是公开可用的站点(只是不可访问).

Heck, try it without the Credential parameter at all, again depending on the site it might be publicly available (just not accessible).

根据网站的不同,他们可能会提供一些API进行下载,请自行决定与他们联系:

Depending on the site they may have some APIs to download it, contact them at your discretion:

$proxy = New-WebServiceProxy -Uri "https://example.com/webservices.asmx" -Credential MyUser
# Again MyUser can be substituted with a credential object
$proxy.GetMyStatement("201607") | Out-File $outFilePath
# Name and syntax depend on how it is designed and may vary wildly from example

最后一招……

#Wait for Download Dialog box to pop up
Sleep 5
while($ie.Busy){Sleep 1} 
#------------------------------
#Hit "S" on the keyboard to hit the "Save" button on the download box
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('s')

#Hit "Enter" to save the file
$obj.SendKeys('{Enter}')

#Closes IE Downloads window
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{Enter}')

请注意,您将需要禁用所有浏览器中的PDF查看器,以便将其视为标准下载,在IE11中,由于它是由PDF查看器管理的,因此可能比较棘手.如果您使用的是Adobe Reader,则需要卸载BrowserIntegration功能.基本上,当您手动单击它时,您想获得运行还是保存?"选项.

Note you will need to disable any in-browser PDF viewers so that it treats it as a standard download, in IE11 this can be tricky as it's managed by the PDF viewers. If you're using Adobe Reader seems you need to uninstall the BrowserIntegration feature. Basically when you manually click on it, you want to get the "Run or Save?" option.

这篇关于使用PowerShell自动执行网站登录和文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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