Automating Saving a file from the web with the IE COM object in Powershell

无人久伴 提交于 2019-12-07 18:19:41
#Start IE and navigate to your download file/location
$ie = New-Object -Com internetExplorer.Application
$ie.Navigate("http://www.your.website.here.com/awesome_stuff/DOC_1.docx")

#------------------------------
#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}')

If you can't parse the javascript and get the url you need you can use wscript to send the key strokes. Note that this is probably a terrible idea and I wouldn't recommend it if there is any way at all to parse the javascript and get the raw url to use a WebClient to download.

$wsh = new-object -comobject wscript.shell
$id = (Get-Process iexplore* | Where-Object{$_.MainWindowTitle -match "YOUR.WEB.PAGE.TITLE.HERE"}).id
if($wsh.AppActivate($id)){
    start-sleep -milliseconds 500
    $wsh.SendKeys("{S}")
    #I don't have IE11 stuck on 8 due to corporate policy you may need a second sendkeys here
    #if so insert a 500 millisecond delay
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!