-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSLogix Cleanup.ps1
More file actions
262 lines (211 loc) · 11.1 KB
/
FSLogix Cleanup.ps1
File metadata and controls
262 lines (211 loc) · 11.1 KB
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
259
260
261
262
<#
.SYNOPSIS
Finds all FSlogix folders in specified directory and cross checks
if the user is disabled, exists and/or is inactive. Based on specified
parameters, will then remove the stale containers from the directory.
.DESCRIPTION
Will automatically clean up stale FSLogix container folders based on
the specified criteria.
.PARAMETER *
Parameters are available as specified in the param block below.
.NOTES
Version: 1.1
Author: Tom Schoen
Creation Date: 01-11-2022
Purpose/Change: Add logging functionality
.EXAMPLE
Remove all containers for disabled, removed/non-existent and inactive users but exclude folders "folder1" and "folder2" from location "F:\" and output logs to "C:\temp".
.\script.ps1 -ContainerPath "F:\" -DeleteDisabled -DeleteRemoved -DeleteInactive -ExcludeFolders @("folder1","folder2") -LogPath "C:\temp"
.EXAMPLE
Remove all containers for disabled users from Azure Files share "\\mystorageaccount.file.core.windows.net\share" and don't ask for confirmation.
.\script.ps1 -ContainerPath "\\mystorageaccount.file.core.windows.net\share" -DeleteDisabled -Confirm
.EXAMPLE
Dry run for removal of all containers for users that have not logged in for 30 days from Azure Files share "\\mystorageaccount.file.core.windows.net\share".
.\script.ps1 -ContainerPath "\\mystorageaccount.file.core.windows.net\share" -DeleteInactive -InactiveDays 30 -WhatIf
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, HelpMessage = "The full (UNC) path to the FSLogix container directory.")]
[string]
$ContainerPath,
[Parameter(HelpMessage = "If set to a full (UNC) path, the script will output the log file to this directory.")]
$LogPath = $False,
[Parameter(HelpMessage = "Name that appears in the name of the log file.")]
[string]
$LogName = "FSLogixCleanUp",
[Parameter(HelpMessage = "If set, enables dry-run mode.")]
[switch]
$WhatIf,
[Parameter(HelpMessage = "Array of strings with folder names to exclude in recursion.")]
[string[]]
$ExcludeFolders,
[Parameter(HelpMessage = "Number of days a user must have not logged into Active Directory to be considered inactive.")]
[int]
$InactiveDays = 90,
[Parameter(HelpMessage = "If set, containers belonging to disabled users will be deleted.")]
[switch]
$DeleteDisabled,
[Parameter(HelpMessage = "If set, containers belonging to removed/non-existing users will be deleted.")]
[switch]
$DeleteRemoved,
[Parameter(HelpMessage = "If set, containers belonging to inactive users will be deleted.")]
[switch]
$DeleteInactive,
[Parameter(HelpMessage = "If set, don't ask for confirmation before execution.")]
[switch]
$Confirm,
[Parameter(HelpMessage = "if set, don't use the FlipFlop name convention (%username%_%sid%) but use the default (%sid%_%username%)")]
[switch]
$NoFlipFlop
)
function Write-Log {
param (
[Parameter(Mandatory)]
[string]
$LogMessage,
[Parameter(Mandatory)]
[string]
$LogLevel,
[Parameter(Mandatory)]
$LogPath,
[Parameter(Mandatory)]
[string]
$LogName,
[string]
$LogHeader,
[switch]
$NoDate
)
if ($NoDate) {
$Message = "[$LogLevel] $LogMessage"
}
else {
$Message = "$(Get-Date -Format "u") [$LogLevel] $LogMessage"
}
if ($LogPath) {
$File = (Join-Path -Path $LogPath -ChildPath "$LogName.log")
if (-not (Test-Path $File) -and $LogHeader) {
Add-Content -Path $File -Value ""
}
Add-content -Path $File -Value $Message
}
Write-Output $Message
}
[decimal]$SpaceDisabled = 0
[decimal]$SpaceRemoved = 0
[decimal]$SpaceInactive = 0
[int]$ContainerCount = 0
$LogName = "$LogName`_$(Get-Date -Format "yyyyMMdd_HHmmss")"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "========================================" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Starting execution" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Container Path: $ContainerPath" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Excluded folders: $ExcludeFolders" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Deletion options" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Removed Users: $DeleteRemoved" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Disabled Users: $DeleteDisabled" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Inactive Users: $DeleteInactive ($InactiveDays days)" -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "========================================" -LogLevel "Info"
if (-not (Test-Path -Path $ContainerPath)) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Container Path not accessible or does not exist." -LogLevel "Error"
Exit
}
if ($True -eq $WhatIf) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Executing with WhatIf switch set. No changes will be made." -LogLevel "Info"
}
elseif ($False -eq $Confirm) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Executing without WhatIf switch set. Specified containers will be deleted." -LogLevel "Info"
$ConfirmTitle = 'Confirm execution'
$ConfirmQuestion = 'Do you want to continue?'
$ConfirmChoices = '&Yes', '&No'
$ConfirmDecision = $Host.UI.PromptForChoice($ConfirmTitle, $ConfirmQuestion, $ConfirmChoices, 1)
if ($ConfirmDecision -eq 1) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Execution stopped by user." -LogLevel "Info"
Exit
}
}
$ContainerDirs = Get-ChildItem -Path $ContainerPath -Directory -Exclude $ExcludeFolders
foreach ($ContainerDir in $ContainerDirs) {
$ContainerCount++
if ($True -eq $NoFlipFlop) {
$UserName = $ContainerDir.Name.Substring($ContainerDir.Name.IndexOf('_') + 1)
}
else {
$UserName = $ContainerDir.Name.Substring(0, $ContainerDir.Name.IndexOf('_S-1-5'))
}
$ContainerDir = Join-Path $ContainerPath $ContainerDir
try {
$ADUser = Get-ADUser -Identity $UserName -Properties sAMAccountName, Enabled, lastLogon, lastLogonDate
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
$ADUser = $False
}
$ContainerSize = (Get-ChildItem -Path $ContainerDir | Measure-Object Length -Sum).Sum / 1Gb
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage ("Processing $UserName ({0:N2} GB)." -f $ContainerSize) -LogLevel "Info"
if ($False -eq $ADUser -and $True -eq $DeleteRemoved) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Account for $UserName does not exist." -LogLevel "Info"
if ($False -eq $WhatIf) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Deleting container for $UserName based on removed/non-existent state of account." -LogLevel "Info"
try {
Remove-Item -Path $ContainerDir -Recurse -Force -ErrorAction Stop
}
catch {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Could not delete container for $UserName`: $($_)" -LogLevel "Warning"
Continue
}
$SpaceRemoved = $SpaceRemoved + $ContainerSize
}
else {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "WhatIf: Deleting container for $UserName based on removed/non-existent state of account." -LogLevel "Info"
$SpaceRemoved = $SpaceRemoved + $ContainerSize
}
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Container deleted for $UserName." -LogLevel "Success"
Continue
}
if ($False -eq $ADUser.Enabled -and $True -eq $DeleteDisabled) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Account for $UserName is disabled." -LogLevel "Info"
if ($False -eq $WhatIf) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Deleting container for $UserName based on disabled state of account." -LogLevel "Info"
try {
Remove-Item -Path $ContainerDir -Recurse -Force -ErrorAction Stop
}
catch {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Could not delete container for $UserName`: $($_)" -LogLevel "Warning"
Continue
}
$SpaceDisabled = $SpaceDisabled + $ContainerSize
}
else {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "WhatIf: Deleting container for $UserName based on disabled state of account." -LogLevel "Info"
$SpaceDisabled = $SpaceDisabled + $ContainerSize
}
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Container deleted for $UserName." -LogLevel "Success"
Continue
}
if ($ADUser.lastLogonDate -lt ((Get-Date).AddDays( - ($InactiveDays))) -and $True -eq $DeleteInactive) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Account for $UserName has been inactive for more than $InactiveDays days." -LogLevel "Info"
if ($False -eq $WhatIf) {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Deleting container for $UserName based on inactive state of account." -LogLevel "Info"
try {
Remove-Item -Path $ContainerDir -Recurse -Force -ErrorAction Stop
}
catch {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Could not delete container for $UserName`: $($_)" -LogLevel "Warning"
Continue
}
$SpaceInactive = $SpaceInactive + $ContainerSize
}
else {
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "WhatIf: Deleting container for $UserName based on inactive state of account." -LogLevel "Info"
$SpaceInactive = $SpaceInactive + $ContainerSize
}
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Container deleted for $UserName." -LogLevel "Success"
Continue
}
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "No action needed for $UserName." -LogLevel "Success"
}
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "Script execution completed" -LogLevel "Success"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "$ContainerCount containers processed." -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "$("{0:N2} GB" -f $SpaceRemoved) reclaimed from removed/non-existent users." -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "$("{0:N2} GB" -f $SpaceDisabled) reclaimed from disabled users." -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "$("{0:N2} GB" -f $SpaceInactive) reclaimed from inactive users." -LogLevel "Info"
Write-Log -LogPath $LogPath -LogName $LogName -LogMessage "$("{0:N2} GB" -f ($SpaceRemoved+$SpaceDisabled+$SpaceInactive)) reclaimed in total." -LogLevel "Info"