从网站ie.document方法下载Powershell文件

时间:2014-09-24 18:47:59

标签: automation download powershell-v3.0 powershell-v4.0

我创建了一个小脚本,使用Internet Explorer 11登录到https网站。我手动鼠标点击将文件下载到IE但是我得到保存打开文件的提示我想成为由于我要在后台运行这个能够绕过这个?我在ie.navigate(url)的末尾错过了一个param来保存文件吗?正如我上面所说,我使用IE11进行此操作并且降级不是一种选择。

任何帮助都会很棒

$username= get-content -Path c:\username.txt
$password= get-content -Path c:\password.txt

$ie = New-Object -ComObject internetExplorer.Application
$ie.Visible= $false
$ie.fullscreen = $false

$ie.Navigate("https://www.mft1.firstdataclients.com/cgi-bin/messageway/mwi.cgi")

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}  

$usernamefield = $ie.Document.getElementByID('user')
$usernamefield.value = $username

$passwordfield = $ie.Document.getElementByID('password')
$passwordfield.value = $password

$ie.document.getElementById("request").click()

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}

$ie.Navigate("https://www.mft1.firstdataclients.com/cgi-bin/messageway/mwi.cgi?    request=ViewDownloaded")

while ($ie.Busy -eq $true){Start-Sleep -seconds 2}


$ie.Document.getElementsByTagName('a')| where-object {$_.href -match "request=TextDownload"} | where-object {$_.href -match 'CL9DFMDE'}| select -First 1 -ExpandProperty 'href'

$link=$ie.Document.getElementsByTagName('a')| where-object {$_.href -match "request=TextDownload"} | where-object {$_.href -match 'CL9DFMDE'}| select -First 1 -ExpandProperty 'href'

$file= $link 

$filename= [string]($file).substring(122)

$filename

$ie.Navigate("$file")

2 个答案:

答案 0 :(得分:2)

我全神贯注地看着它,唯一的方法是假装"点击"在保存按钮上。我用以下代码完成了这个:

#------------------------------
#Wait for Download Dialog box to pop up
Sleep 10
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')

#------------------------------
#Wait for Download to complete
Sleep 10
while($ie.Busy){Sleep 1} 
#------------------------------

答案 1 :(得分:1)

您可以使用Invoke-WebRequest来保存文档,如下所示:

Invoke-WebRequest -Uri "URL_OF_Download_file" -Outfile "save_location"