0

I need to compare a file in SharePoint against a local file and to make sure they are the same version (it's an accdb that's developed offline and shared in multiple client SharePoint locations). I need to get the modified date from the file in SharePoint, and I've been unable to do so.

NB: I don't have Get-SPWeb available, as this needs to be run by various users who won't have SharePoint 2013 Management Shell available to them (reference).

I've tried using Get-PnPFile with -AsListItem, and it doesn't return an error but I can't access any properties.

I've tried .FieldValuesAsText and .ListItemAllFields and not getting anywhere.

This bit, connecting to the SharePoint resource via SharePointPnPPowerShellOnline, is working:

$SiteURL = "https://myorg.sharepoint.com/sites/mysite"
$FileRelativeURL = "Shared Documents/mydb.accdb"
$UserName = "user@myorg.org"
$PlainPassword = "myP@ssw0rd#"
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

I tried this, and it downloads the file; I don't need to do that:

$file1 = Get-PnPFile -Url $FileRelativeURL -AsFile

According to this, "-AsListItem" returns the file as a listitem showing all its properties. So I tried it. It didn't download the file, and it didn't return an error, so maybe I have an object I can query now?

$file1 = Get-PnPFile -Url $FileRelativeURL -AsListItem
write-host $file1

The second line returns Microsoft.SharePoint.Client.ListItem.

The following return empty strings:

write-host $file1.Name
write-host $file1.TimeLastModified
write-host $file1.LastModified
write-host $file1[-1]
write-host $file1[0,1,2,3,4,5,6,7,8,9]

Found FieldValuesAsText mentioned somewhere, so I tried it:

$props = $file1.FieldValuesAsText
write-host $props
write-host $props[0]

The first one returns Microsoft.SharePoint.Client.FieldStringValues; the second one returns an empty string.

Tried ListItemAllFields:

$props = $file1.ListItemAllFields
write-host $props
write-host $props[0]

The first line didn't generate an error; the second line returned an empty string; the third line returned:

Cannot index into a null array.
At line:1 char:1
+ write-host $props[0]
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Very new to PowerShell, and I'm probably missing something obvious. (SharePointPnPPowerShellOnline is installed and working.)

The code above doesn't include getting information from my local file; I haven't gotten that far yet. I imagine the instructions here will work. My issue is getting the metadata from the file in SharePoint.

Grateful for any assistance or pointing me to the right resource; I've done a couple hours of research and am coming up empty.

southtexasmike
  • 69
  • 1
  • 13
  • Additionally tried `$file1 | Get-Member` after reading this: https://www.codykonior.com/2013/03/26/powershell-how-to-show-all-of-an-objects-properties-and-values/. I got a _lot_ of output. But don't know what to do with it. None of the output says, "This is the ModifiedDate." :-) – southtexasmike Sep 10 '19 at 11:40
  • I think your best bet would be to map the SharePoint location to a `PSDrive` according to [this](https://social.technet.microsoft.com/Forums/en-US/829dfd78-7737-4f95-84b0-ed6acaff4932/how-to-get-the-uploaded-documents-last-modified-date?forum=sharepointgenerallegacy) by using `New-PSDrive`. After that you can easily access it as a normal file system and consult the `LastWriteTime` property. – DarkLite1 Sep 10 '19 at 11:57
  • And, _that_ would be why I can't find how to do this documented anywhere. Thanks for this. If you post it as an answer, I'll mark it. Or perhaps I should delete the question? – southtexasmike Sep 10 '19 at 12:06

1 Answers1

0

Your best bet would be to map the SharePoint drive as a PSDrive on your system. Then you can treat it like a regular file system and the property LastWriteTime will be available.

More info on New-PSDrive can be found in the documentation online or by typing Get-Help New-PSDrive in a PowerShell console/ISE.

DarkLite1
  • 13,637
  • 40
  • 117
  • 214