How do I save a pdf file using Powershell?

by karmista at 2013-01-01 05:54:00

Using the below IE Application object, I am logging into a http website by providing username and password & attempting to download pdf files.
new-object -com InternetExplorer.Application

I have over a million files that needs to be downloaded.

Currently, I get a “Find/ Save/ Cancel” prompt.

How do I save the pdf file without getting this prompt?
If the prompt cannot be avoided, there should be some way I can programatically press the save button & provide a name and save the file.
(Sendkeys method did not work well)

Attempted Browsers: IE 8 & IE 9



$wshell = New-Object -ComObject WScript.Shell
$id = (gps iexplore* | where {$_.MainWindowTitle -match “MyProg”}).id
Write-host $id
$wshell.AppActivate($id)
start-sleep -milliseconds 100;
$wshell.SendKeys("%{S}");
by DonJ at 2013-01-01 09:22:06
Not using PowerShell. Besides the fact that you’re going through several layers to get to that COM object, it just isn’t what PowerShell’s designed for. If you had to do this with IE, I’d consider exploring AutoIt, or something else designed for that. PowerShell would want you to go down a more programmatic approach and not use IE at all. You’d create a WebRequest, feed it the URL, get the response back as a binary stream, and then write that binary stream to a file. It’s a bit more complicated from a programming perspective, but it’s more reliable. IE’s goofy.

http://www.codeguru.com/columns/dotnett … lasses.htm has the basic methodology, although the code is in C# and you’d need to translate. You can also do some searching for “powershell curl” and “powershell wget” - folks have solved this before in various ways. v3 has Invoke-WebRequest, which may shortcut the process a bit if you’re using v3.
by karmista at 2013-01-02 04:52:34
Thanks Donj!

Will explore Invoke-WebRequest

The C# code looks puzzling as I’m a windows server admin. PowerShell comes in naturally. :slight_smile:

Using powershell, I had almost got to the point where I get a save prompt. Thought if I can just get that working, it’d be perfect.
Yes, I can use PowerShell V3 to take the advantage of its workflows as well.
by karmista at 2013-01-02 05:04:51
If I had a clear cut download url without any login required, I could have used the below System.Net.WebClient object.

PS > $source = "http://www.leeholmes.com/favicon.ico"
PS > $destination = "c:\temp\favicon.ico"
PS >
PS > $wc = New-Object System.Net.WebClient
PS > $wc.DownloadFile($source, $destination)

Source: http://answers.oreilly.com/topic/2006-h … owershell/

However, my scenario is:

1) Login to a website via providing credentials in the application i.e. within the username/password text box.
2) Post a url
EG: http://10.12.1.12/myapp/myfile.aspx?docid=49000
3) A standard pdf file gets generated i.e. myfile.pdf when I send this request from an authenticated IE browser session.
4) Save this pdf

5) Rename it to 49000.pdf [Here, 49000 is the doc id.]
6) Repeat steps 2 to 5 a million times to download all the docs.