2

I am using Powershell DownloadFile(,) command for reading a .csv file from a document library in SharePoint 2013 site. When i ran the code on my DEV environment, it ran properly. The DEV environment is using http://. But , when I run this code on TEST environment i get 403 Forbidden error. The site(TEST environment) is running over https://.

I am using below code ,

$FilePath = "https://ServerName/sites/SiteName/MyDocumentLibrary/MyCSVFile.csv"
$localPath = "C:\temp\test.csv" 
$wc = New-Object System.Net.Webclient
$wc.DownloadFile($FilePath, $localPath)
$list = Import-Csv $localPath

The error says,

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (403) Forbidden." At D:\Amit\UpdateUserInformationList.ps1:39 char:1

I have gone through various articles which talks about passing credentials with the Webclient($wc) object. But, with no luck. Still getting same error.

I am still unaware, when/why does Powershell throws this error ? What is the root cause of this error?

Any help will be greatly appreciated.

  • Have you tried setting the UserAgent as part of $wc? – Amal Hashim Dec 15 '14 at 17:52
  • I don't think so...can you provide an sample. – Amit Tyagi Dec 15 '14 at 17:59
2

Try this

$wc = New-Object System.Net.WebClient
$wc = New-Object System.Net.Networkcredential(Username, Password)

$UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
$wc.Headers.Add([System.Net.HttpRequestHeader]::UserAgent, $UserAgent);
  • I am getting a warning WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers. and the file is created at C:\temp\test.csv. But the file contains the HTML of the ADFS provider selection page(login page), instead of the actual content expected. – Amit Tyagi Dec 15 '14 at 18:32
  • @AmitTyagi see my update.. Try passing the credentials also – Amal Hashim Dec 15 '14 at 18:34
  • No success. Do i need to pass the domain also with the credentials. As i am using ADFS and the credentials i am using are not of AD. – Amit Tyagi Dec 15 '14 at 19:01
  • Thanks Amal, for investing time. I got the solution as mentioned in the answer posted by me. – Amit Tyagi Dec 15 '14 at 23:37
2

After a long day, it turned out that for successfully using the Powershell DownloadFile() method, in my environment (SP2013 with ADFS) following two lines did the trick(at least for me).

$wc.UseDefaultCredentials = $true
$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

So, the final code looks like,

$FilePath = "https://ServerName/sites/SiteName/MyDocumentLibrary/MyCSVFile.csv"
$localPath = "C:\temp\test.csv" #Make sure the folder do exist
$wc = New-Object System.Net.Webclient
$wc.UseDefaultCredentials = $true
$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$wc.DownloadFile($FilePath, $localPath)
$list = Import-Csv $localPath

I got help from the last comment(which is not marked as answer for that question :-)) of this post.

  • This will work if you are accessing remote resource from your own credentials. – Kaushal Khamar Feb 10 '16 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.