tags:

views:

263

answers:

5

Hi, i am working in a script, where I am able to browse the web content or the 'url' but I am not able to copy the web content in it & download as a file. This is what I have made so far:

$url = "http://sp-fin/sites/arindam-sites/_layouts/xlviewer.aspx?listguid={05DA1D91-F934-4419-8AEF-B297DB81A31D}&itemid=4&DefaultItemOpen=1"
$ie=new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate($url)
while($ie.busy) {start-sleep 1} 

How can i copy the content of $url & save it to local drive as a file?

Regards Arindam

+5  A: 

I'm not aware of any way to save using that interface.

Does this render the page properly:

PS>$wc=new-object system.net.webclient
PS>$wc.downloadfile("your_url","your_file")
Marco Shaw
+1  A: 

If you just want to download web content, use

(New-Object System.Net.WebClient).DownloadFile( 'http://stackoverflow.com/questions/1973880/download-url-content', 'save.html' )

Jay Bazuzi
A: 

Hi Marco, here is what I get as error:

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:7 char:17 + $wc.downloadfile( <<<< "$url","birthday.xlsx")

Regards Arindam

Arindam
A: 

Hi Jay, when I tried your method, I got these errors:

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url/download-url-content", 'save.html' )

Missing ')' in method call. At :line:6 char:68 + (New-Object system.net.webclient).DownloadFile( "$url", 'save.html' <<<<

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:6 char:47 + (New-Object system.net.webclient).DownloadFile( <<<< "$url", 'save.html' )

Ok, let me explain more, on what I am trying to do: I have a excel file in our share point site & this is the file I am trying to download locally(any format), which is a part of the script, so that for the later part of the script, I can compare this file with other data & get an output.

Now if I can somehow map "my documents" from the site & able to download the file, that will also work for me.

Thanks & Regards Arindam

Arindam
Edit your question to reflect the updated information...
Peter Seale
A: 

As I understand it, you try to use IE because if automatically sends your credentials (or maybe you didn't know of any other option).

Why the above answers don't work is because you try to download file from SharePoint and you send an unauthenticated request. The response is 401.

This works:

PS>$wc=new-object system.net.webclient
PS>$wc.UseDefaultCredentials = $true
PS>$wc.downloadfile("your_url","your_file")

if the the current user of Posh has rights to download the file (is the same as the logged one in IE).

If not, try this:

PS>$wc=new-object system.net.webclient
PS>$wc.Credentials = Get-Credential
PS>$wc.downloadfile("your_url","your_file")
stej