3

Context

Using PowerShell, I was hoping to schedule a task to pull down files from specific folders on SharePoint automatically every morning. It seems fairly simple, but I cannot even manage to connect to the SharePoint. The SharePoint is located online, and I login with my Active Directory information.

I have PowerShell and have been running PowerShell ISE. I also have the SharePoint Online Management Shell and SharePoint Client Components installed.

The first code I try is:

$fromfile = "https://domain.com/sites/project/Shared%20Documents/folder1/folderA/test.pdf"
$tofile   = "C:\test.pdf"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.DownloadFile($fromfile, $tofile)

But I get a 403 forbidden error. My guess is it has something to do with my credentials. I tried another code, this time using the SharePoint shell features:

$fromsite = "https://domain.com/sites/project"

$fromfile = "Shared%20Documents/folder1/folderA/test.pdf"
$tofile   = "C:\test.pdf"

$web = Get-SPWeb $fromsite
$file = $web.GetFile($fromfile)
$filebytes = $file.OpenBinary()

$filestream = New-Object System.IO.FileStream($tofile, "Create")
$binarywriter = New-Object System.IO.BinaryWriter($filestream)
$binarywriter.write($filebytes)
$binarywriter.Close()

This gives me there error: Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

So then I try running:

Add-PSSnapin Microsoft.Sharepoint.Powershell

But then I get Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 4. So for good measure I check the following

Get-PSSnapin -Registered

Unsurprisingly, the list is blank. This means that I need to somehow register those Snapins. This leads me to this site: https://blogs.msdn.microsoft.com/opal/2010/03/07/sharepoint-2010-with-windows-powershell-remoting-step-by-step/

I have followed everything successfully, until Get-WSManCredSSP. I get this response: The machine is not configured to allow delegating fresh credentials. This computer is configured to receive credentials from a remote client computer.. So I followed everything on here https://technet.microsoft.com/en-us/hh751273.aspx and I cannot figure out where to go from here.

Request

  • Is there a better way to write a script to pull these files? Perhaps one that wouldn't cause any of these issues?
  • If not, how can I get SharePoint functionality in PowerShell? How can I get the SharePoint snap-ins to be registered?

Thank you for your help!

1

I was unable to get it to work using PowerShell with this method. I believe it's because I am not running the commands on the SharePoint server itself.

Instead, I created a batch script with the following:

net use X: "\\sharepoint.site@SSL\DavWWWRoot\sites\project\Shared Documents\folder 1\folder 2"
robocopy "X:" %dest% /LEV:2 /s 

(%dest% being a variable I defined earlier)

0

Try to add X-FORMS_BASED_AUTH_ACCEPTED header with f value using following code: $webclient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f") before executing $webclient.DownloadFile().

According to Microsoft.SharePoint.PowerShell snap-in and Get-SPWeb cmdlet - they can be used only directly on SharePoint server.

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.