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:
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
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.
- 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!