Link to home
Start Free TrialLog in
Avatar of jramprakash
jramprakashFlag for New Zealand

asked on

Powershell - Downloading file from website

Hi Experts,

I am trying to download file from the SharePoint portal.

I was able to login successfully and open the file , but unable to download the file. Could you please advise. Thanks.

$username="username"
$password="password"
$url = "https://mycompany.com/sites/folder/document.xlsx"
$destination  = "c:\temp\document.xlsx"

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.navigate($url)

Start-Sleep -Milliseconds 10000
while($ie.Busy){Sleep 1} 

$ie.Document.getElementByID("Ecom_User_ID").value = $username
$ie.Document.getElementByID("Ecom_Password").value = $password

Invoke-WebRequest -Uri $url -Outfile $destination
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')
$obj.SendKeys('s')
$obj.SendKeys('{Enter}')

Open in new window

Avatar of Piotr Strycharz
Piotr Strycharz
Flag of Poland image

Use WebClient class instead:

$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential "username", "password"
$data = $webClient.DownloadData($url)
[System.IO.File]::WriteAllBytes($destination, $data)
$webClient.Dispose()

Open in new window

Avatar of jramprakash

ASKER

Hi Piotr,

I tried similar code earlier but its not working. I get the below error
"The remote server returned an error: (401) Unauthorized."

Open in new window

$source = "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx"
$destination = "c:\temp\SomeExcelFile.xlsx"
$user = user_name
$pwd = password
$wc = new-object System.Net.WebClient

$wc.UseDefaultCredentials = $true


$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials

$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$wc.DownloadFile($source, $destination)
While it's not clear whether you did use my code or your sample. But... There might be multiple reasons:

  • you provided bad user name or password
  • you did not specify domain in credentials
  • you have some proxy between you and the server
  • you actually did not have access to the fie

Run this one-liner and check the results:

Invoke-WebRequest -Uri "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx" -Credential (Get-Credential)

Open in new window

Hi Piotr,

Thank you for the quick response. The credentials work fine when I try the website manually and without any domain details. The credentials also work fine for the initial code I placed in my question.

I tried the below code, but I got the below error message.

$site = "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx"
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials  (Get-Credential)

Open in new window


Error message:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.

However, when I try this, I do not have any issues. Please advice

$site = "https://www.google.com/"
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials 

Open in new window

You are missing "credential" parameter in Invoke-WebRequest.

Should be:
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials  -Credential (Get-Credential)

Open in new window

not:
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials  (Get-Credential)

Open in new window

Hello Piotr,

I tried the following code and I got the StatusCode response ‘200’.

$cred = (Get-Credential)
$site = "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx"
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials -Credential $cred

Open in new window


However, when I try the below, I did find ‘excel.xlsx’ file in the destination. However,when I tied to open the file, Excel was throwing the error that it cannot open the file as its in ‘unrecognized’ format. When I open the file in ‘notepad’, it was a bunch of HTML code. What could be the issue? Thank you.

  Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080'  -ProxyUseDefaultCredentials -Credential $cred -OutFile c:\temp\excel.xlsx

Open in new window

That's closer. That means that you succesfully provided URL and connected to the server. The HTML code is probably 'Access denied' page (you may confirm this first). If so - it is very likely that you will be able to run this command and get the results:

Invoke-WebRequest -Uri "https://share.mycompany.com/sites/department/_api/web/currentuser"
  -Proxy 'http://proxyserver:8080'  -ProxyUseDefaultCredentials -Credential (Get-Credential) -OutFile c:\temp\response.txt

Open in new window


If you get "Invoke-WebRequest : The remote server returned an error: (401) Unauthorized." it means that the account does not have any access to the site. If the user has at least limited access to site (that means user has access to some list or item inside site) - you get the results. Check the results and share your thoughts.
Hi Piotr,

I ran the code you gave me. I got the response.txt file. Its exactly the same file I got earlier when I used the below cmdlet to extract the excel file.
  Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080'  -ProxyUseDefaultCredentials -Credential $cred -OutFile c:\temp\excel.xlsx

I saw the below lines in the HTML code that I got in the response. I see that ‘active scripting’ is already enabled on my machine. Not sure why this complaining. Thank you.

“We're sorry, but javascript has not been enabled on your browser. Please enable javascript before returning to the site.”
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.