Created
October 8, 2019 19:43
-
-
Save joerodgers/15ff35de37e76d6679e083d84cc54488 to your computer and use it in GitHub Desktop.
Downloads a file, deletes it, then uploads the same file with updated properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-PSSnapin -Name Microsoft.SharePoint.Powershell | |
function Get-SPFile | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory=$true)][string]$FileUrl | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
try | |
{ | |
$site = New-Object Microsoft.SharePoint.SPSite($FileUrl) | |
$web = $site.OpenWeb() | |
$web.GetFile($FileUrl) | |
} | |
finally | |
{ | |
if( $web ) | |
{ | |
$web.Dispose() | |
} | |
if( $site ) | |
{ | |
$site.Dispose() | |
} | |
} | |
} | |
end | |
{ | |
} | |
} | |
function Replace-SPFile | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory=$true)][Microsoft.SharePoint.SPFile]$File, | |
[parameter(Mandatory=$true)][string]$DefaultLogin | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
# attempt to use current author | |
if( $File.Author ) | |
{ | |
$author = $File.Author | |
} | |
else | |
{ | |
$author = $File.ParentFolder.ParentWeb.EnsureUser($DefaultLogin) | |
} | |
# attempt to use current editor | |
if( $File.ModifiedBy ) | |
{ | |
$editor = $File.ModifiedBy | |
} | |
else | |
{ | |
$editor = $File.ParentFolder.ParentWeb.EnsureUser($DefaultLogin) | |
} | |
# pull other props | |
$filesCollection = $File.ParentFolder.Files | |
$timeCreated = $File.TimeCreated | |
$timeLastModified = $File.TimeLastModified | |
$serverRelativeUrl = $File.ServerRelativeUrl | |
$properties = @{} | |
try | |
{ | |
$stream = $File.OpenBinaryStream() | |
$bytes = New-Object byte[] $($stream.Length) | |
$stream.Read($bytes, 0, $stream.Length) | Out-Null | |
} | |
finally | |
{ | |
if( $stream ) | |
{ | |
$stream.Dispose() | |
} | |
} | |
# delete current file | |
$File.Delete() | |
# replace file with new props | |
$filesCollection.Add($serverRelativeUrl, $bytes, $properties, $author, $editor, $timeCreated, $timeLastModified, $true ) | Out-Null | |
} | |
end | |
{ | |
} | |
} | |
$defaultLogin = "i:0#.w|contoso\adamb" | |
$file = Get-SPFile -FileUrl "https://sharepoint.2016.contoso.com/sites/teamsite/shared documents/book1.xlsx" | |
Write-Host "Staring Author:$($file.Properties.vti_author)" | |
Write-Host "Staring Editor:$($file.Properties.vti_modifiedby)" | |
if( $file.Exists ) | |
{ | |
Replace-SPFile -File $file -DefaultLogin $defaultLogin | |
} | |
$file = Get-SPFile -FileUrl "https://sharepoint.2016.contoso.com/sites/teamsite/shared documents/book1.xlsx" | |
Write-Host "Ending Author:$($file.Properties.vti_author)" | |
Write-Host "Ending Editor:$($file.Properties.vti_modifiedby)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment