-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathAuto-ApplyRetentionLabels.PS1
258 lines (238 loc) · 12.5 KB
/
Auto-ApplyRetentionLabels.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Auto-ApplyRetentionLabels.PS1
# An example of how to apply retention labels to files in SharePoint Online and OneDrive for Account sites
# V1.0 December 2024
# GitHub Link: https://github.com/12Knocksinna/Office365itpros/blob/master/Auto-ApplyRetentionLabels.PS1
# Requires the following permissions
# RecordsManagement.ReadWrite.All (retention labels)
# Sites.Read.All (to access sites)
# Files.Read.All (to access files) (Sites.Read.All includes Files.Read.All)
function Get-DriveItems {
[CmdletBinding()]
param (
[Parameter()]
$Drive,
[Parameter()]
$FolderId
)
# Get data for a folder and its children, check each file to see if it has a retention label. If it has, and the
# retention threshold is not met, apply the default retention label defined in the script
[array]$Data = Get-MgDriveItemChild -DriveId $Drive -DriveItemId $FolderId -All
# Split the data into files and folders
[array]$Folders = $Data | Where-Object {$_.folder.childcount -gt 0} | Sort-Object Name
[array]$Files = $Data | Where-Object {$null -ne $_.file.mimetype}
# Process the files
ForEach ($File in $Files) {
# Get retention label information
$FileExtension = $File.Name.Split(".")[1].ToUpper()
If ($FileExtension -notin $SupportedFileTypes) {
continue
}
If ($File.CreatedDateTime) {
$FileCreatedDateTime = Get-Date $File.CreatedDateTime -format 'dd-MMM-yyyy HH:mm'
}
# Is the file already older than the retention period? If so, we leave it alone
If ($File.LastModifiedDateTime) {
[datetime]$LastModifiedDateTime = Get-Date $File.LastModifiedDateTime
If ($LastModifiedDateTime -lt $RetentionDuration) {
Write-Host ("File {0} last modified {1} is older than retention period" -f $File.Name, $LastModifiedDateTime)
$ReportLine = [PSCustomObject]@{
TimeStamp = (Get-Date -format s)
FileName = $File.Name
Folder = $File.parentreference.name
Created = $FileCreatedDateTime
Author = $File.CreatedBy.User.DisplayName
LastModified = Get-Date $LastModifiedDateTime -format 'dd-MMM-yyyy HH:mm'
'Last modified by' = $File.LastModifiedBy.User.DisplayName
'Retention label' = $RetentionLabel['Name']
Path = $Site.WebUrl
Action = "Retention label not applied - modified date older than retention period"
}
$ReportData.Add($ReportLine)
Continue
}
} Else {
$LastModifiedDateTime = $null
}
Try {
$FileInfo = Get-MgDriveItemRetentionLabel -DriveId $Drive -DriveItemId $File.Id -ErrorAction Stop
} Catch {
Write-Host ("Error reading retention label data from file {0}" -f $File.Name)
Continue
}
If ([string]::IsNullOrEmpty($FileInfo.Name)) {
# Attempt to apply the defined retention label
If (!$PreviewMode) {
$Status = Update-MgDriveItemRetentionLabel -DriveId $Drive -DriveItemId $File.Id -BodyParameter $RetentionLabel
If ($Status.Name) {
Write-Host ("Retention label assigned to {0}" -f $File.Name) -ForegroundColor DarkGray
$ReportLine = [PSCustomObject]@{
TimeStamp = (Get-Date -format s)
FileName = $File.Name
Folder = $File.parentreference.name
Created = $FileCreatedDateTime
Author = $File.CreatedBy.User.DisplayName
LastModified = Get-Date $LastModifiedDateTime -format 'dd-MMM-yyyy HH:mm'
'Last modified by' = $File.LastModifiedBy.User.DisplayName
'Retention label' = $RetentionLabel['Name']
Path = $Site.WebUrl
Action = "Retention label applied"
}
$ReportData.Add($ReportLine)
} Else {
Write-Host ("Unable to assign retention label to {0}" -f $File.Name) -ForegroundColor Red
Continue
}
} Else {
# Preview Mode, so just log the fact that we would have applied the label
$ReportLine = [PSCustomObject]@{
TimeStamp = (Get-Date -format s)
FileName = $File.Name
Folder = $File.parentreference.name
Created = $FileCreatedDateTime
Author = $File.CreatedBy.User.DisplayName
LastModified = Get-Date $LastModifiedDateTime -format 'dd-MMM-yyyy HH:mm'
'Last modified by' = $File.LastModifiedBy.User.DisplayName
'Retention label' = $RetentionLabel['Name']
Path = $Site.WebUrl
Action = "Retention label not applied (preview mode)"
}
$ReportData.Add($ReportLine)
}
}
}
# Process the folders found in the root
ForEach ($Folder in $Folders) {
Write-Host ("Processing folder {0}" -f $Folder.Name) -ForegroundColor Green
Get-DriveItems -Drive $Drive -FolderId $Folder.Id
}
}
# Start of Main Script
param (
[switch]$Global:PreviewMode
)
if ($PreviewMode) {
Write-Host "Running in preview mode. No changes will be made."
}
# Start of real work
Connect-MgGraph -Scopes RecordsManagement.ReadWrite.All, Sites.Read.All
$LocationsFile = 'c:\temp\Locations.csv'
If (!(Test-Path $LocationsFile)) {
Write-Host "Locations file for unlabeled files not found - exiting"
Break
}
[array]$AllLocations = Import-Csv -Path $LocationsFile
# Exclude all OneDrive for Business sites
[array]$Locations = $AllLocations | Where-Object {$_.Location -notlike "*my.sharepoint.com/personal*"}
# Define default retention label to apply
$Global:RetentionLabel = @{}
$RetentionLabel.Add("Name","General Purpose Information")
$Global:RetentionDuration = (Get-Date).AddYears(-3)
# Supported file types that we will apply retention labels to
$Global:SupportedFileTypes = "DOCX", "PPTX", "XLSX", "PDF"
# Output PowerShell lists for reports
$Global:ReportData = [System.Collections.Generic.List[Object]]::new()
$Global:ProblemSites = [System.Collections.Generic.List[Object]]::new()
# Let people know what we plan to do
If ($PreviewMode) {
Write-Host "Running in preview mode. No retention labels will be applied to files."
}
# Try to access each site. If we can gain access, look for unlabeled files
ForEach ($Location in $Locations) {
$Uri = $Location.Location; $Site = $null; $LookupUri = $null
# Create a value that we can find with a call to Get-MgSite. The resulting value will be something like
# office365itpros.sharepoint.com:/sites/SeniorTeam
# $LookUpUri = $Uri.Split('//')[1].split("/")[0] + ":/sites/" + $Uri.Split('//')[1].split("/")[2]
Try {
$Global:Site = Get-MgSite -Search $Uri -ErrorAction Stop
} Catch {
# Try to find the site with the site name
Try {
$Global:Site = Get-MgSite -Search $Uri.Split("/sites/")[1] -ErrorAction Stop
} Catch {
Write-Host ("Unable to access site {0} {1}" -f $Uri, $_.Exception.Message) -ForegroundColor Red
# Log the problem site
$ProblemSiteReportLine = [PSCustomObject]@{
TimeStamp = (Get-Date -format s)
Site = $Uri
Action = "Unable to access site"
}
$ProblemSites.Add($ProblemSiteReportLine)
Continue
}
}
If (!$Site) {
Write-Host ("Site {0} not found" -f $Uri) -ForegroundColor Red
Continue
} Else {
Write-Host ("Processing site {0} to look for unlabeled files" -f $Site.DisplayName) -ForegroundColor Yellow
}
Try {
[array]$AllDrives = Get-MgSiteDrive -SiteId $Site.Id -ErrorAction Stop
} Catch {
Write-Host ("Unable to access drives in site {0} ({1}) {2}" -f $Site.DisplayName, $URI, $_.Exception.Message) -ForegroundColor Red
$ProblemSiteReportLine = [PSCustomObject]@{
TimeStamp = (Get-Date -format s)
Site = $Uri
Action = "Unable to access drives"
}
$ProblemSites.Add($ProblemSiteReportLine)
Continue
}
[array]$Drives = $AllDrives | Where-Object {$_.Name -notlike "*Preservation Hold Library*" -and $_.Name -notlike "*Teams Wiki Data*"}
If (!$Drives) {
Write-Host "No drives found in site $URI"
Continue
}
# Process each drive
ForEach ($Drive in $Drives) {
Write-Host ("Processing drive {0} in site {1}" -f $Drive.Name, $Site.DisplayName) -ForegroundColor Cyan
Get-DriveItems -Drive $Drive.Id -FolderId "root"
}
}
Write-Host "The following sites could not be processed"
$ProblemSites | Format-Table Site, Action -AutoSize
[array]$SitesWithLabeledFiles = $ReportData | Where-Object {$_.Action -eq 'Retention label applied'} | Select-Object -ExpandProperty Path | Sort-Object -Unique
Write-Host ""
If ($PreviewMode) {
Write-Host "Retention Labeling Run Statistics (Preview mode)"
Write-Host "------------------------------------------------"
} Else {
Write-Host "Retention Labeling Run Statistics"
Write-Host "--------------------------------"
}
Write-Host ""
Write-Host ("Retention label used: {0}" -f $RetentionLabel['Name'])
Write-Host ("Retention date threshold: {0} ({1} days)" -f $RetentionDuration, (New-TimeSpan -Start $RetentionDuration).Days)
Write-Host ("Total {0} of files processed in {1} locations" -f $ReportData.Count, ($Locations.count - $ProblemSites.count))
If ($PreviewMode) {
Write-Host ("Retention labels not applied because of preview mode: {0}" -f ($ReportData | Where-Object {$_.Action -eq "Retention label not applied (preview mode)"}).Count)
} Else {
Write-Host ("Retention labels applied: {0}" -f ($ReportData | Where-Object {$_.Action -eq "Retention label applied"}).Count)
}
Write-Host ("Files older than the threshold to apply retention label: {0}" -f ($ReportData | Where-Object {$_.Action -eq "Retention label not applied - modified date older than retention period"}).Count)
Write-Host ""
Write-Host "Sites where labels were applied:"
$SitesWithLabeledFiles
Write-Host ""
Write-Host "Generating report..."
If (Get-Module ImportExcel -ListAvailable) {
$ExcelGenerated = $True
Import-Module ImportExcel -ErrorAction SilentlyContinue
$ExcelOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Retention Labeling.xlsx"
If (Test-Path $ExcelOutputFile) {
Remove-Item $ExcelOutputFile -ErrorAction SilentlyContinue
}
$ReportData | Export-Excel -Path $ExcelOutputFile -WorksheetName "Retention Labeling Report" -Title ("Retention Labeling Report") -TitleBold -TableName "RetentionLabels"
} Else {
$CSVOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Retention Labeling.CSV"
$ReportDtra | Export-Csv -Path $CSVOutputFile -NoTypeInformation -Encoding Utf8
}
If ($ExcelGenerated) {
Write-Host ("An Excel report is available in {0}" -f $ExcelOutputFile)
} Else {
Write-Host ("A CSV report is available in {0}" -f $CSVOutputFile)
}
# An example script used to illustrate a concept. More information about the topic can be found in the Office 365 for IT Pros eBook https://gum.co/O365IT/
# and/or a relevant article on https://office365itpros.com or https://www.practical365.com. See our post about the Office 365 for IT Pros repository # https://office365itpros.com/office-365-github-repository/ for information about the scripts we write.
# Do not use our scripts in production until you are satisfied that the code meets the need of your organization. Never run any code downloaded from the Internet without
# first validating the code in a non-production environment.