0

I need a PowerShell (v2) script to read or copy files from a SharePoint library resident on a remote SharePoint server. The twist is, I need to filter on the last modified date of the file(s). Also, the server running the script does not have SharePoint installed.

Here's what I've tried:

  1. Get-ChildItem using the UNC path of the SP file with LastWriteTime filter. I encountered latency issues with this. It runs fine if I've recently opened the document library in the web. But when scheduled to run on its own, the read consistently fails (cannot find file).

  2. Copying the files with the script below. Problem is that the modified date is lost when it's copied to the target machine. The date added/modified is now the copy date.

    $target = "http://myspweb.com/myfile.xlsx"
    $dest   = "D:\library\newfile.xlsx"
    $wc = New-Object System.Net.WebClient
    $wc.UseDefaultCredentials = $true
    $wc.DownloadFile($target, $dest)
    
  3. Using the Get-SPWeb command. Allegedly, this exposes properties of the files, including last modified date, but seems to require that the server running the script have SharePoint installed (Add-PSSnapin Microsoft.Sharepoint.Powershell gives a "not installed on this machine" error.) A full SharePoint installation isn't really an option -- nor is remoting. Some installation of SP tools might be an option, but I can't figure out what would be required.

Any other ways to approach this?

Syphirint
  • 1,021
  • 2
  • 13
  • 24
bvy
  • 144
  • 2
  • 12
  • Have you tried using New-PSDrive to mount the location in your session and then accessing the file(s)? I currently use that method to mount a SharePoint Form Library to read the forms (XML files). It works consistently for me. – Jon Dechiro Aug 22 '16 at 17:59
  • @Jon, perhaps, but I think I run into the same issue as I outlined in #1 above, because I'm accessing it with a UNC path. I may try it though. – bvy Aug 22 '16 at 19:53

1 Answers1

0

What you can't do (server-side object model)

The Get-SPWeb cmdlet can only be used from one of the web front end servers that are actually running SharePoint as part of your SharePoint farm, so if you don't have access to log in to the server, that option is right out.

What you can do (web services)

Alternatively, SharePoint exposes web services that you can use to access SharePoint data (including column data) from a remote server.

The available web services vary depending on which version of SharePoint you are using.

The Lists Web Service

The SharePoint 2010/2013 Lists.asmx web service can be accessed if you're willing to compose your own SOAP calls. You can refer to this answer for an example that uses Powershell. Refer to the official documentation here.

You can either format your query so that it only returns results with a certain modified date (using CAML query syntax), or you can return all the results and process them in Powershell to select the ones you care about.

The REST API

SharePoint 2013 also has a REST-based web service you can use. (Technically, SharePoint 2010 does as well, but it's a bit half-baked.) Here's an example using Powershell and the REST API: https://gallery.technet.microsoft.com/office/How-to-Get-all-the-Lists-a0af6259

Community
  • 1
  • 1
Thriggle
  • 7,009
  • 2
  • 26
  • 37