-
Notifications
You must be signed in to change notification settings - Fork 3
/
Restore-FromRecycleBin.ps1
110 lines (78 loc) · 3.85 KB
/
Restore-FromRecycleBin.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#Requires -Modules PnP.PowerShell, ImportExcel
#DRAFT VERSION!!
<#
.SYNOPSIS
Restore-FromRecycleBin enumerates the contents of a SPO site's recycle bin and restores items after a given date, logging the results.
This script was designed to handle a large number of items and aims to be more reliable than the native browser-based restore functionality.
.DESCRIPTION
Restore-FromRecycleBin enumerates the contents of a SPO site's recycle bin and restores items after a given date, logging the results.
This script was designed to handle a large number of items and aims to be more reliable than the native browser-based restore functionality.
Features:
- Before it starts, the script writes a file to disk containing all of the files it is about to restore.
- During the run, there is a counter and other output describing the progress.
- When complete, a log file is written, which contains every file attempted and a true/false for its success.
The most common failure we've seen is due to the fact a file already exists, which can be confirmed with use of the log file.
Requirements:
1) SharePoint Online. This was not written for use with SharePoint Server.
2) PnP.PowerShell and ImportExcel modules
3) PowerShell 5.0+
4) Permission to restore items from the site
5) Interactive session. This is written to require an administrator to be present and enter the credentials interactivly.
March 22 2021
-Mike Crowley
-Jhon Ramirez
.EXAMPLE
Restore-FromRecycleBin -SiteUrl https://MySpoSite.sharepoint.com/sites/Site123 -RestoreDate 3/23/2021 -LogDirectory C:\Logs
.LINK
https://BaselineTechnologies.com
#>
function Restore-FromRecycleBin
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory=$true)] [string] $SiteUrl,
[Parameter(Mandatory=$true)] [datetime] $RestoreDate,
[Parameter(Mandatory=$true)] [string] $LogDirectory
)
$VerbosePreference = 'Continue'
$ScriptDate = Get-Date -format ddMMMyyyy_HHmm.s
$ExportParams =
@{
AutoSize = $true
AutoFilter = $true
BoldTopRow = $true
FreezeTopRow = $true
}
$SpoConnection = Connect-PnPOnline -Url $SiteUrl -Interactive -ReturnConnection
mkdir $LogDirectory -Force
$RecycleBinFiles = Get-PnPRecycleBinItem -Connection $SpoConnection -FirstStage | Where-Object {[datetime]$_.DeletedDateLocalFormatted -gt $RestoreDate} | Where-Object {$_.ItemType -eq "File"}
Write-Verbose ("Found " + ($RecycleBinFiles.count) + " files.")
$RecycleBinFiles | Export-Excel @ExportParams -Path ("$LogDirectory\RecycleBinFiles\" + "RecycleBinFiles--" + $ScriptDate + ".xlsx")
$LogFile = @()
$LoopCounter = 1
foreach ($File in $RecycleBinFiles)
{
Write-Verbose ("Attempting to Restore: " + $File.Title + " to: " + $File.DirName + "`n")
Write-Verbose ("$LoopCounter" + " of " + $RecycleBinFiles.Count + "`n")
$LoopCounter ++
$RestoreSucceeded = $true
try {Restore-PnpRecycleBinItem -Force -Identity $File.Id.Guid -ErrorAction Stop}
catch {$RestoreSucceeded = $false }
$LogFile += '' | Select-Object @(
@{N="RestoreAttempt"; e={Get-Date -UFormat "%D %r"}}
@{N="RestoreSucceeded"; e={$RestoreSucceeded}}
@{N="FileName"; e={$File.Title}}
@{N="DirName"; e={$File.DirName}}
@{N="OriginalDeletionTime"; e={$File.DeletedDateLocalFormatted}}
@{N="Id"; e={$File.Id}}
)
switch ($RestoreSucceeded)
{
$true {Write-Verbose ("Restored: " + ($File.Title))}
$false {Write-Verbose ("ERROR: " + $Error[0].ErrorDetails)}
}
}
$LogFile | Export-Excel @ExportParams -Path ("$LogDirectory\RecycleBinFiles\" + "RestoreLog--" + $ScriptDate + ".xlsx")
}
#