-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderRedirectionPaths.psm1
More file actions
331 lines (284 loc) · 13.3 KB
/
FolderRedirectionPaths.psm1
File metadata and controls
331 lines (284 loc) · 13.3 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#Requires -Version 5.1
# Global variables
$script:MaxPathLength = 247
$script:OneDriveBasePath = "C:\Users\{0}\OneDrive - {1}" # {0} = username, {1} = tenant name
# Known folder mappings for OneDrive
$script:OneDriveFolderMappings = @{
'My Documents' = 'Documents'
'Documents' = 'Documents' # Include both variations
'My Pictures' = 'Pictures'
'My Music' = 'Music'
'My Video' = 'Videos'
'Desktop' = 'Desktop' # These don't change but included for completeness
}
# OneDrive incompatible characters and file names
$script:InvalidChars = @('<', '>', ':', '"', '|', '?', '*', '\')
$script:InvalidNames = @(
'CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9',
'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9', '_vti_', 'desktop.ini'
)
function Write-LogMessage {
[CmdletBinding()]
param(
[string]$Message,
[ValidateSet('Info', 'Warning', 'Error')]
[string]$Level = 'Info'
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Level] $Message"
switch ($Level) {
'Info' { Write-Verbose $logMessage }
'Warning' { Write-Warning $logMessage }
'Error' { Write-Error $logMessage }
}
}
function Test-OneDriveCompatibility {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
$issues = @()
$fileName = Split-Path $Path -Leaf
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
# Check for invalid characters
foreach ($char in $script:InvalidChars) {
if ($fileName.Contains($char)) {
$issues += "Contains invalid character: '$char'"
}
}
# Check for invalid file names
if ($script:InvalidNames -contains $fileNameWithoutExtension.ToUpper()) {
$issues += "Reserved file name: '$fileNameWithoutExtension'"
}
# Check for files starting or ending with periods or spaces
if ($fileName.StartsWith('.') -and $fileName.Length -gt 1) {
$issues += "File name starts with period"
}
if ($fileName.EndsWith('.') -or $fileName.EndsWith(' ')) {
$issues += "File name ends with period or space"
}
# Check file name length (255 character limit for file names)
if ($fileName.Length -gt 255) {
$issues += "File name too long (>255 characters)"
}
return [PSCustomObject]@{
Path = $Path
FileName = $fileName
IsCompatible = $issues.Count -eq 0
Issues = $issues
}
}
function Test-FolderRedirectionPaths {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$RootPath,
[Parameter(Mandatory = $true)]
[string]$TenantName,
[Parameter(Mandatory = $false)]
[string[]]$UserName,
[Parameter()]
[switch]$Summary,
[Parameter()]
[switch]$CheckCompatibility,
[Parameter()]
[switch]$EnableLogging,
[Parameter()]
[switch]$ShowProgress
)
begin {
# Helper function to conditionally write log messages
function Write-OptionalLog {
param([string]$Message, [string]$Level = 'Info')
if ($EnableLogging) {
Write-LogMessage -Message $Message -Level $Level
}
}
function Write-OptionalProgress {
param(
[int]$Id,
[string]$Activity,
[string]$Status,
[int]$PercentComplete,
[string]$CurrentOperation
)
if ($ShowProgress) {
Write-Progress -Id $Id -Activity $Activity -Status $Status -PercentComplete $PercentComplete -CurrentOperation $CurrentOperation
}
}
Write-OptionalLog "Starting folder redirection path scan for root path: $RootPath"
$results = @()
$userSummary = @{}
$folderItemCounts = @{}
}
process {
try {
# Validate root path exists
if (-not (Test-Path $RootPath)) {
throw "Root path does not exist: $RootPath"
}
# Initialize data structures
$results = @()
$userSummary = @{}
$folderItemCounts = @{}
# Use provided usernames or scan for user folders
if ($UserName) {
$userFolders = @()
foreach ($name in $UserName) {
$userPath = Join-Path $RootPath $name
if (-not (Test-Path $userPath)) {
Write-Warning "User folder does not exist and will be skipped: $userPath"
continue
}
$userFolders += [PSCustomObject]@{
Name = $name
FullName = $userPath
LocalUsername = $name
}
}
if ($userFolders.Count -eq 0) {
throw "None of the specified user folders exist in $RootPath"
}
} else {
$userFolders = Get-ChildItem -Path $RootPath -Directory | ForEach-Object {
$localUsername = $_.Name
if ($localUsername -match '^[^\\]+\\(.+)$') {
$localUsername = $matches[1]
}
[PSCustomObject]@{
Name = $_.Name
FullName = $_.FullName
LocalUsername = $localUsername
}
}
}
$userCount = $userFolders.Count
$currentUserIndex = 0
foreach ($userFolder in $userFolders) {
$currentUsername = $userFolder.Name
$currentUserIndex++
# Show overall progress for users
$userPercentComplete = ($currentUserIndex / $userCount) * 100
Write-OptionalProgress -Id 0 -Activity "Scanning User Folders" -Status "Processing user $currentUserIndex of $userCount" `
-PercentComplete $userPercentComplete -CurrentOperation "Current User: $currentUsername"
Write-OptionalLog "Processing user folder: $currentUsername"
$userSummary[$currentUsername] = @{
}
$oneDriveBasePath = $script:OneDriveBasePath -f $userFolder.LocalUsername, $TenantName
Write-OptionalLog "OneDrive base path will be: $oneDriveBasePath"
$userPath = $userFolder.FullName
$topLevelFolders = Get-ChildItem -Path $userPath -Directory -ErrorAction SilentlyContinue
foreach ($folder in $topLevelFolders) {
Write-OptionalLog "Processing folder: $($folder.Name)"
$userSummary[$currentUsername][$folder.Name] = 0
$folderKey = "$currentUsername`_$($folder.Name)"
$folderItemCounts[$folderKey] = 0
# Map folder name to OneDrive equivalent using predefined mappings
$oneDriveFolderName = if ($script:OneDriveFolderMappings.ContainsKey($folder.Name)) {
$script:OneDriveFolderMappings[$folder.Name]
} else {
$folder.Name
}
$oneDriveFolderPath = Join-Path $oneDriveBasePath $oneDriveFolderName
$allItems = Get-ChildItem -Path $folder.FullName -Recurse -ErrorAction SilentlyContinue
$totalItems = if ($allItems) { $allItems.Count } else { 0 }
$folderItemCounts[$folderKey] = $totalItems
# Initialize item counter for progress
$currentItemIndex = 0
foreach ($item in $allItems) {
$currentItemIndex++
# Show progress for current folder scan
if ($totalItems -gt 0) {
$itemPercentComplete = ($currentItemIndex / $totalItems) * 100
Write-OptionalProgress -Id 1 -Activity "Scanning Items in $($folder.Name)" `
-Status "Processing item $currentItemIndex of $totalItems" `
-PercentComplete $itemPercentComplete `
-CurrentOperation $item.Name
}
$relativePath = $item.FullName.Substring($folder.FullName.Length).TrimStart('\')
$oneDriveItemPath = Join-Path $oneDriveFolderPath $relativePath
$pathLength = $oneDriveItemPath.Length
$exceedsLimit = $pathLength -gt $script:MaxPathLength
$charactersOver = if ($exceedsLimit) { $pathLength - $script:MaxPathLength } else { 0 }
# Only check compatibility if the switch is provided
$compatibilityResult = if ($CheckCompatibility) {
Test-OneDriveCompatibility -Path $item.FullName
} else {
[PSCustomObject]@{
Path = $item.FullName
FileName = Split-Path $item.FullName -Leaf
IsCompatible = $true
Issues = @()
}
}
$result = [PSCustomObject]@{
UserName = $currentUsername
FolderName = $folder.Name
Path = $item.FullName
OneDrivePath = $oneDriveItemPath
PathLength = $pathLength
ExceedsLimit = $exceedsLimit
CharactersOverLimit = $charactersOver
IsCompatible = $compatibilityResult.IsCompatible
Issues = ($compatibilityResult.Issues -join '; ')
RequiresAction = $exceedsLimit -or (-not $compatibilityResult.IsCompatible)
}
$results += $result
if ($exceedsLimit) {
Write-OptionalLog "Path exceeds limit: $oneDriveItemPath (Length: $pathLength, Over by: $charactersOver)" -Level Warning
$userSummary[$currentUsername][$folder.Name]++
}
if ($CheckCompatibility -and -not $compatibilityResult.IsCompatible) {
Write-OptionalLog "Compatibility issue: $($item.FullName) - $($compatibilityResult.Issues -join ', ')" -Level Warning
}
}
}
}
# Clear both progress bars
if ($ShowProgress) {
Write-Progress -Id 1 -Activity "Scanning Items" -Completed
Write-Progress -Id 0 -Activity "Scanning User Folders" -Completed
}
$totalItems = $results.Count
$itemsWithIssues = ($results | Where-Object { $_.RequiresAction }).Count
Write-OptionalLog "Scan completed. Found $itemsWithIssues issues in $totalItems items."
if ($Summary) {
if ($EnableLogging) {
Write-Verbose "`nPer-User Path Length Summary:"
Write-Verbose "-----------------------------"
}
$summaryObjects = @()
foreach ($user in $userSummary.Keys | Sort-Object) {
Write-OptionalLog "`nUser: $user"
foreach ($folder in $userSummary[$user].Keys | Sort-Object) {
$count = $userSummary[$user][$folder]
$folderKey = "$user`_$folder"
$totalItemsInFolder = $folderItemCounts[$folderKey]
if ($count -gt 0) {
Write-OptionalLog " $folder : $count paths over limit" -Level Warning
} else {
Write-OptionalLog " $folder : No issues"
}
$summaryObjects += [PSCustomObject]@{
UserName = $user
Folder = $folder
PathsOverLimit = $count
HasIssues = $count -gt 0
TotalItems = $totalItemsInFolder
}
}
}
return $summaryObjects
} else {
return $results
}
}
catch {
Write-Error "Error during folder redirection scan: $($_.Exception.Message)"
throw
}
}
}
# Export only the main function
Export-ModuleMember -Function 'Test-FolderRedirectionPaths'