使用Powershell登录到URL并下载文件

时间:2017-10-27 13:31:03

标签: powershell url download

我无法创建Powershell脚本来登录网站并下载文件。该脚本似乎正好登录(我收到成功的状态返回)但是当我尝试下载该文件时,我收到了一条未经授权的消息作为回报。

代码如下:

$r=Invoke-WebRequest http://testurl/index.htm -SessionVariable fb
$form = $r.Forms[0]
$form.fields["loginusername"] = "user"
$form.fields["loginpassword"] = "pass"
$r=Invoke-WebRequest -Uri ("https://testurl/index.htm" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields
Invoke-WebRequest -Uri "https://testurl/api/table.csv" -WebSession $fb -OutFile "C:\status\current.csv"

感谢任何指导!

1 个答案:

答案 0 :(得分:0)

我看过你试图引用的例子。当我试图复制它时,它有一些错误。我已经自动化了一些网站登录/下载;这是一个调整尝试:

$r=Invoke-WebRequest http://testurl/index.htm
$r.Forms.fields.loginusername = 'user'
$r.Forms.fields.loginpassword = 'pass'

Invoke-WebRequest -Uri 'https://testurl' + $form.Action) -SessionVariable fb -Method POST -Body $r
Invoke-WebRequest -Uri 'https://testurl/api/table.csv' -WebSession $fb -OutFile 'C:\status\current.csv'
相关问题