jramprakash
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.
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}')
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."
$destination = "c:\temp\SomeExcelFile.xls x"
$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.NetworkCredenti al($user,$ pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCac he]::Defau ltNetworkC redentials
$wc.Headers.Add("X-FORMS_B ASED_AUTH_ ACCEPTED", "f")
$wc.DownloadFile($source, $destination)
I tried similar code earlier but its not working. I get the below error
"The remote server returned an error: (401) Unauthorized."
$source = "https://share.mycompany.com/sites/department/SomeExcelFile.xlsx"$destination = "c:\temp\SomeExcelFile.xls
$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.NetworkCredenti
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials
$wc.Headers.Add("X-FORMS_B
$wc.DownloadFile($source, $destination)
While it's not clear whether you did use my code or your sample. But... There might be multiple reasons:
Run this one-liner and check the results:
- 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)
ASKER
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.
Error message:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'System.Management.Automat ion.PSCred ential'.
However, when I try this, I do not have any issues. Please advice
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)
Error message:
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'System.Management.Automat
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
You are missing "credential" parameter in Invoke-WebRequest.
Should be:
Should be:
Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials -Credential (Get-Credential)
not:Invoke-WebRequest -Uri $site -Proxy 'http://proxyserver:8080' -ProxyUseDefaultCredentials (Get-Credential)
ASKER
Hello Piotr,
I tried the following code and I got the StatusCode response ‘200’.
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.
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
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
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:
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.
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
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.
ASKER
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' -ProxyUseDefaultCredential s -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.”
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' -ProxyUseDefaultCredential
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 TRIALMembers 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.
Open in new window