2

I have a sharepoint 2013 server, which I can log into using Windows Authentication via a web-browser. When I have logged on using my browser, if I then - using that same browser - browse to http://mysharepointserver/_api/web I get some useful XML.

However, I want to write a powershell script that uses this XML. To that end, following the suggestions from this page: How to make an authenticated web request in Powershell?

I wrote the following code:

$PWord = ConvertTo-SecureString –String "MyAwesomePassword" –AsPlainText -Force
$creds = New-Object System.Net.NetworkCredential("MyUserName",$PWord,"TheDomain")
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $creds
$url = "http://mysharepointserver/_api/web"
$output = $webclient.DownloadString($url)
echo $output

However, when I run this code, I get the error message:

Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (403) Forbidden."

Even though I can access the same URL using a web-browser, if I type in the (same) credentials using the Windows login dialog that pops up for authentication purposes.

Does anyone know if it is possible to do this using Powershell?

thanks heaps all

Community
  • 1
  • 1
  • I'm not sure I understand you correctly, but if this xml-file is available in a SharePoint Document Library, you can just map the Document Library as a Windows mapped drive with the correct credentials and access it from there. – DarkLite1 Oct 27 '15 at 10:02

1 Answers1

-1

Why aren't you using the default CMDlets that come with Powershell (unless using PS2 or lower)? Invoke-Webrequest and Invoke-Restmethod both support the -credential switch which can be used to authenticate against the webserver. You can also use Get-credential to store your credentials (though you can't get yhem from a file or string that way, at least not the password)

Script (in pseudo code and since i'm on my phone no code tags) would look like this : Invoke-Restmethod -uri 'http://bla/api' -credential (get-credential)

bluuf
  • 936
  • 1
  • 6
  • 14
  • Hi, the main reason I'm not using commandlets like Invoke-Webrequest, is that I don't know how to do this - I'm learning by googling. I've actually tried the Invoke-Webrequest (and several other commandlets but have not yet had success). What I'm trying to achieve here is a console application that will retrieve documents from a series of lists (across a firewall where only port 80 is open), but step 1 is to find out what lists are on the sharepoint server - thus the experimentation with the rest api to retrieve the information. The problem I'm having so far is programmatic authentication. – David Buddrige Oct 29 '15 at 02:11