-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCleanUpSystem.ps1
753 lines (589 loc) · 24.3 KB
/
CleanUpSystem.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#!/usr/bin/env pwsh
####
# Cleanup Window System
####
# @see:
# https://github.com/Bromeego/Clean-Temp-Files/blob/master/Clear-TempFiles.ps1
# https://gist.github.com/synikil/47784f432979d97e5dc1181349e1e591
# @since 2021-04-06
# @author stev leibelt <[email protected]>
####
Class Logger {
#bo properties
hidden [bool] $BeVerbose
hidden [int] $GlobalLogLevel
hidden [string] $Path
#eo properties
#bo functions
Logger (
[string] $Path,
[int] $GlobalLogLevel,
[bool] $BeVerbose
) {
$this.BeVerbose = $BeVerbose
$this.GlobalLogLevel = $GlobalLogLevel
$this.Path = $Path
}
[void] Debug(
[string] $Message
) {
$this.LogLine($Message, 0)
}
[void] Error(
[string] $Message
) {
$this.LogLine($Message, 4)
}
[void] Info(
[string] $Message
) {
$this.LogLine($Message, 2)
}
[void] LogLine(
[string] $Message,
[int] $LogLevel
) {
If ($LogLevel -ge $this.GlobalLogLevel) {
$Prefix = "[None]"
Switch ($LogLevel)
{
0 { $Prefix = "[Trace]"; Break }
1 { $Prefix = "[Debug]"; Break }
2 { $Prefix = "[Information]"; Break }
3 { $Prefix = "[Warning]"; Break }
4 { $Prefix = "[Error]"; Break }
5 { $Prefix = "[Critical]"; Break }
Default { $Prefix = "[None]"; Break }
}
$CurrentDateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$CurrentLogLine = '{0}: {1} - {2}' -f $CurrentDateTime,$Prefix,$Message
Add-Content -Path $this.Path -Value $CurrentLogLine
If ($this.BeVerbose) {
Write-Host $CurrentLogLine -ForegroundColor DarkGray
}
}
}
#eo functions
}
Class TruncableObject
{
#bo properties
hidden [bool] $CheckForDuplicates
hidden [int] $CheckForDuplicatesGreaterThanMegabyte
hidden [int] $DaysToKeepOldFiles
hidden [string] $Path
#eo properties
#bo functions
TruncableObject (
[bool] $CheckForDuplicates,
[int] $CheckForDuplicatesGreaterThanMegabyte,
[int] $DaysToKeepOldFiles,
[string] $Path
) {
$this.Path = $Path
$this.DaysToKeepOldFiles = $DaysToKeepOldFiles
$this.CheckForDuplicates = $CheckForDuplicates
$this.CheckForDuplicatesGreaterThanMegabyte = $CheckForDuplicatesGreaterThanMegabyte
}
[bool] Get-CheckForDuplicates()
{
return $this.CheckForDuplicates
}
[int] Get-CheckForDuplicatesGreaterThanMegabyte()
{
return $this.CheckForDuplicatesGreaterThanMegabyte
}
[int] Get-DaysToKeepOldFiles()
{
return $this.DaysToKeepOldFiles
}
[string] Get-Path()
{
return $this.Path
}
[void] Set-Path(
[string] $Path
) {
$this.Path = $Path
}
#eo functions
}
Function New-TruncableObject
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Path,
[Parameter(Mandatory = $false)]
[int] $DaysToKeepOldFiles = 1,
[Parameter(Mandatory = $false)]
[bool] $CheckForDuplicates = $false,
[Parameter(Mandatory = $false)]
[int] $CheckForDuplicatesGreaterThanMegabyte = 64
)
return [TruncableObject]::new(
$CheckForDuplicates,
$CheckForDuplicatesGreaterThanMegabyte,
$DaysToKeepOldFiles,
$Path
)
}
Function New-LockFileOrExit {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $lockFilePath,
[Parameter(Mandatory = $true)]
[Logger] $Logger
)
If (Test-Path $lockFilePath) {
Write-Host ":: Lock file exists. Maybe an previous run was stopped or is crashed."
$YesOrNo = Read-Host -Prompt " Should I remove it? (y|N)"
If ($YesOrNo.StartsWith("y") -eq $true) {
$Logger.Debug("Removing existing lock file >>${lockFilePath}<< after asking the user.")
Remove-Item -Path $lockFilePath
} Else {
Write-Error ":: Error"
Write-Error " Could not aquire lock, lock file >>${lockFilePath}<< exists."
Write-ErrorLogAndExit $Logger "Could not aquire lock. Lock file >>${lockFilePath}<< exists." ($CurrentExitCodeCounter++)
}
}
New-Item -ItemType File $lockFilePath
Set-Content -Path $lockFilePath -Value "${PID}"
$Logger.Debug("Lock file create, path >>${lockFilePath}<<, content >>${PID}<<")
}
Function Remove-LockFileOrExit {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $lockFilePath,
[Parameter(Mandatory = $true)]
[Logger] $Logger
)
If (Test-Path $lockFilePath) {
$lockFilePID = Get-Content -Path $lockFilePath
If ($lockFilePID -eq $PID ){
Remove-Item -Path $lockFilePath
$Logger.Debug("Lock file removed, path >>${lockFilePath}<<")
} Else {
Write-Error ":: Error"
Write-Error " Lockfile in path >>${lockFilePath}<< contains different PID. Expected >>${PID}<<, Actual >>${lockFilePID}<<."
Write-ErrorLogAndExit $Logger "Lockfile in path >>${lockFilePath}<< contains different PID. Expected >>${PID}<<, Actual >>${lockFilePID}<<." ($CurrentExitCodeCounter++)
}
} Else {
Write-Error ":: Error"
Write-Error " Could not release lock. Lock file >>${lockFilePath}<< does not exists."
Write-ErrorLogAndExit $Logger "Could not release lock. Lock file >>${lockFilePath}<< does not exists." ($CurrentExitCodeCounter++)
}
}
Function Get-LogFilePath {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$path
)
If (!(Test-Path $path)) {
New-Item -ItemType Directory -Force -Path $path
}
$date = Get-Date -Format "yyyyMMdd"
$pathToTheLogFile = '{0}\{1}_{2}.log' -f $path,$env:computername,$date
return $pathToTheLogFile
}
Function Remove-ItemAndLogResult {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $ItemToRemove,
[Parameter(Mandatory = $false)]
[bool] $BeRecursive = $false,
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $false)]
[bool] $IsDryRun = $false
)
If ($IsDryRun) {
$Logger.Debug(" Would try to remove item >>${ItemToRemove}<<.")
} Else {
$Logger.Debug(" Trying to remove item >>${ItemToRemove}<<.")
If ($BeRecursive -eq $true) {
Remove-Item -Path "$ItemToRemove" -Force -Recurse -ErrorAction SilentlyContinue
} Else {
Remove-Item -Path "$ItemToRemove" -Force -ErrorAction SilentlyContinue
}
$RemoveItemWasSucessful = $?
If ($RemoveItemWasSucessful -ne $true) {
$Logger.Info(" Item could not be removed.")
}
}
}
Function Write-LogMessage {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$path,
[Parameter(Mandatory = $true)]
[string]$message,
[Parameter(Mandatory = $true)]
[int]$logLevel = 3,
[Parameter(Mandatory = $false)]
[bool]$beVerbose = $false
)
If ($logLevel -ge $globalLogLevel) {
Switch ($logLevel) {
0 { $prefix = "[Trace]" }
1 { $prefix = "[Debug]" }
2 { $prefix = "[Information]" }
3 { $prefix = "[Warning]" }
4 { $prefix = "[Error]" }
5 { $prefix = "[Critical]" }
default { $prefix = "[None]" }
}
$dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logLine = '{0}: {1} - {2}' -f $dateTime,$prefix,$message
Add-Content -Path $path -Value $logLine
If ($beVerbose) {
Write-Host $logLine -ForegroundColor DarkGray
}
}
}
Function Write-ErrorLogAndExit {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $true)]
[string] $LogMessage,
[Parameter(Mandatory = $true)]
[int] $ExitCode
)
Logger.Error($LogMessage)
Exit $ExitCode
}
Function Create-DiskInformation
{
$logicalDisk = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" }
$totalSizeInGB = "{0:N1}" -f ( $logicalDisk.Size / 1gb)
$freeSizeInGB = "{0:N1}" -f ( $logicalDisk.Freespace / 1gb )
$freeSizeInPercentage = "{0:P1}" -f ( $logicalDisk.FreeSpace / $logicalDisk.Size )
$properties = @{
device_id = $logicalDisk.DeviceId
total_size_in_gb = $totalSizeInGb
free_size_in_gb = $freeSizeInGB
free_size_in_percentage = $freeSizeInPercentage
}
$object = New-Object psobject -Property $properties
return $object
}
Function Create-StatisticObject {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.TimeSpan]$runDatetime,
[Parameter(Mandatory = $true)]
[int]$numberOfRemovedFileSystemObjects,
[Parameter(Mandatory = $true)]
[object]$startDiskInformation,
[Parameter(Mandatory = $true)]
[object]$endDiskInformation
)
$startFreeSizeInGB = $startDiskInformation.free_size_in_gb -replace ",", "."
$endFreeSizeInGB = $endDiskInformation.free_size_in_gb -replace ",", "."
$properties = @{
runtime = @{
hours = $runDatetime.Hours
minutes = $runDatetime.Minutes
seconds = $runDatetime.Seconds
}
disk = @{
number_of_removed_file_system_objects = $numberOfRemovedFileSystemObjects
freed_up_disk_space = ($endFreeSizeInGB - $startFreeSizeInGB)
}
}
$object = New-Object psobject -Property $properties
return $object
}
Function Delete-RecycleBin {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Logger] $Logger
)
$Logger.Info(":: Starting deletion of recycle bin.")
$IsOlderThanWindowsServer2016 = ( [System.Environment]::OSVersion.Version.Major -lt 10 );
If ( $IsOlderThanWindowsServer2016 -eq $true ) {
If ($beVerbose -eq $true) {
Start-Process -FilePath rd -ArgumentList '/s' '/q' 'C:\$Recycle.Bin' -Wait
} Else {
Start-Process -FilePath rd -ArgumentList '/s' 'C:\$Recycle.Bin' -Wait
}
} Else {
If ($beVerbose -eq $true) {
Clear-RecycleBin -Force -Verbose
} Else {
Clear-RecycleBin -Force
}
}
$Logger.Info(":: Finished deletion of recycle bin.")
}
Function Write-DiskspaceLog {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $true)]
[object] $DiskInformation
)
$Logger.Info("Drive: {0}, Total Size (GB) {1}, Free Size (GB) {2}, Free size in percentage {3}" `
-f $DiskInformation.device_id, $DiskInformation.total_size_in_gb, $DiskInformation.free_size_in_gb, $DiskInformation.free_size_in_percentage)
}
Function Write-StatisticLog
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $true)]
[object] $StatisticObject
)
$Logger.Info(":: Statistics ::")
$Logger.Info(" Runtime: Hours >>{0}<<, Minutes >>{1}<<, Seconds >>{2}<<." `
-f $StatisticObject.runtime.hours, $StatisticObject.runtime.minutes, $StatisticObject.runtime.seconds)
$Logger.Info(" Freed up disk space >>{0}<< Number of removed file system objects >>{1}<<." `
-f $StatisticObject.disk.freed_up_disk_space, $StatisticObject.disk.number_of_removed_file_system_objects)
}
Function Start-CleanUpSystem
{
#bo: variable definition
$currentDate = Get-Date -Format "yyyyMMdd"
$collectionOfTruncableObjects = New-Object System.Collections.ArrayList
$globalConfigurationFilePath = ($PSScriptRoot + "\data\globalConfiguration.ps1")
$localConfigurationFilePath = ($PSScriptRoot + "\data\localConfiguration.ps1")
#We have to source the files here and not via a function.
# If we would source the files via a function, the sourced in variables would exist in the scope of the function only.
If ((Test-Path $globalConfigurationFilePath)) {
. $globalConfigurationFilePath
} Else {
Write-Error "Could not find path to global configuration >>${globalConfigurationFilePath}<<. Global configuration is mandatory!"
Exit ($CurrentExitCodeCounter++)
}
#we are creating a logger to have at least one running
$logFilePath = Get-LogFilePath $logDirectoryPath
$Logger = [Logger]::new($LogFilePath, $globalLogLevel, $beVerbose)
If ((Test-Path $localConfigurationFilePath)) {
. $localConfigurationFilePath
} Else {
$Logger.Info("Could not find path to local configuration >>${localConfigurationFilePath}<<. This run withouts local configuration!")
}
#we have to create a second instance since there is a chance that one of the three variables have been changed
$logFilePath = Get-LogFilePath $logDirectoryPath
$Logger = [Logger]::new($logFilePath, $globalLogLevel, $beVerbose)
#eo: variable definition
#bo: clean up
$startDateTime = (Get-Date)
$startDiskInformation = Create-DiskInformation
New-LockFileOrExit $lockFilePath $Logger
Write-DiskspaceLog $Logger $startDiskInformation
#disable windows update service to enable cleanup of >>c:\windows\softwaredistribution<<
Get-Service -Name wuauserv | Stop-Service -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Verbose
# bo: manipulating the file system
$numberOfRemovedFileSystemObjects = Start-PathTruncations $collectionOfTruncableObjects $Logger $beVerbose
If ($deleteRecycleBin -eq $true) {
Delete-RecycleBin $Logger
}
If ($startDiskCleanupManager -eq $true) {
Start-DiskCleanupManager $Logger
}
# eo: manipulating the file system
$runDateTime = (Get-Date).Subtract($startDateTime)
$endDiskInformation = Create-DiskInformation
$statisticObject = Create-StatisticObject $runDatetime $numberOfRemovedFileSystemObjects $startDiskInformation $endDiskInformation
Write-DiskspaceLog $Logger $endDiskinformation
Write-StatisticLog $Logger $statisticObject
#enable windows update service
Get-Service -Name wuauserv | Start-Service -ErrorAction SilentlyContinue
Remove-LockFileOrExit $lockFilePath $Logger
#eo: clean up
}
Function Start-DiskCleanupManager {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[Logger] $Logger
)
$Logger.Info(":: Starting system disk cleanup manager.")
#@see: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cleanmgr
If ($beVerbose -eq $true) {
Start-Process -FilePath Cleanmgr -ArgumentList '/sagerun:1', '/VERYLOWDISK' -Wait -Verbose
} Else {
Start-Process -FilePath Cleanmgr -ArgumentList '/sagerun:1', '/verylowdisk' -Wait
}
$Logger.Info(":: Finished system disk cleanup manager.")
}
Function Start-PathTruncation {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[TruncableObject] $TruncableObject,
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $true)]
[int]$numberOfRemovedFileSystemObjects,
[Parameter(Mandatory = $false)]
[bool]$isDryRun = $false
)
$DisplayProgessBar = ($beVerbose -ne $true)
$ProcessPath = $true
$ProcessedFileItemCounter = 1
#if path ends with >>\*<<
$pathEndsWithAStar = ($TruncableObject.Get-Path() -match '\\\*$')
$pathWithoutStarAtTheEnd = $path.Substring(0, $path.Length-1)
If ($pathEndsWithAStar) {
#if path does not contain another wild card
If (!$pathWithoutStarAtTheEnd.Contains('*')) {
If (!(Test-Path $pathWithoutStarAtTheEnd)) {
$Logger.Info("Path does not exist >>" + $TruncableObject.Get-Path() + "<<. Skipping it.")
$ProcessPath = $false
}
}
} Else {
If (!(Test-Path $TruncableObject.Get-Path())) {
$Logger.Info("Path does not exist >>" + $TruncableObject.Get-Path() + "<<. Skipping it.")
$ProcessPath = $false
}
}
If ($ProcessPath) {
$Logger.Info("Truncating path >>" + $TruncableObject.Get-Path() + "<< with days to keep files older than >>" + $TruncableObject.Get-DaysToKeepOldFiles() + "<< days.")
#if we have to check against last modification date
If ($TruncableObject.Get-DaysToKeepOldFiles() -ne 0) {
$lastPossibleDate = (Get-Date).AddDays(-$TruncableObject.Get-DaysToKeepOldFiles())
$Logger.Info(" Removing entries older than >>${lastPossibleDate}<<.")
$matchingItems = Get-ChildItem -Path $TruncableObject.Get-Path() -Recurse -ErrorAction SilentlyContinue |
Where-Object LastWriteTime -lt $lastPossibleDate
$numberOfItemsToRemove = $matchingItems.Count
$Logger.Info(" Removing >>${numberOfItemsToRemove}<< entries.")
ForEach ($matchingItem In $matchingItems) {
If ($DisplayProgessBar -eq $true){
Write-Progress -Activity ":: Removing items." `
-Status "[${ProcessedFileItemCounter} / ${numberOfItemsToRemove}]" `
-PercentComplete (($ProcessedFileItemCounter / $numberOfItemsToRemove) * 100) `
-Id 1 `
-ParentId 0
++$ProcessedFileItemCounter
}
$FullQualifiedPath = Join-Path -Path $pathWithoutStarAtTheEnd -ChildPath $matchingItem
Remove-ItemAndLogResult "${FullQualifiedPath}" $true $Logger $isDryRun
++$numberOfRemovedFileSystemObjects
}
} Else {
$Logger.Info(" Removing all entries, no date limit provided.")
$matchingItems = Get-ChildItem -Path $TruncableObject.Get-Path() -Recurse -ErrorAction SilentlyContinue
$numberOfItemsToRemove = $matchingItems.Count
If (($DisplayProgessBar -eq $true) -and ($numberOfItemsToRemove -gt 0)){
Write-Progress -Activity ":: Removing items." `
-Status "[${ProcessedFileItemCounter} / ${numberOfItemsToRemove}]" `
-PercentComplete (($ProcessedFileItemCounter / $numberOfItemsToRemove) * 100) `
-Id 1 `
-ParentId 0
++$ProcessedFileItemCounter
}
$Logger.Info(" Removing >>${numberOfItemsToRemove}<< entries.")
$FullQualifiedPath = Join-Path -Path $pathWithoutStarAtTheEnd -ChildPath $matchingItem
Remove-ItemAndLogResult "${FullQualifiedPath}" $true $Logger $isDryRun
++$numberOfRemovedFileSystemObjects
}
If ($TruncableObject.Get-CheckForDuplicates()) {
$listOfFileHashToFilePath = @{}
$matchingFileSizeInByte = $TruncableObject.Get-CheckForDuplicatesGreaterThanMegabyte() * 1048576 #1048576 = 1024*1024
$Logger.Debug("Checking for duplicates with file size greater than >>${matchingFileSizeInByte}<< bytes.")
$matchingItems = Get-ChildItem -Path $TruncableObject.Get-Path() -Recurse -File -ErrorAction SilentlyContinue |
Where-Object Length -ge $matchingFileSizeInByte
$numberOfItemsToRemove = $matchingItems.Count
If ($matchingItems.Count -gt 1 ) {
$Logger.Info(" Checking >>${numberOfItemsToRemove}<< entries of being duplicates.")
ForEach ($matchingItem In $matchingItems) {
$filePathToMatchingItem = $($TruncableObject.Get-Path() + "\${matchingItem}")
$Logger.Debug(" Processing matching item file path >>${filePathToMatchingItem}<<.")
If (Test-Path -Path $filePathToMatchingItem) {
$fileHashObject = Get-FileHash -Path "$filePathToMatchingItem" -Algorithm SHA256
$fileHash = $fileHashObject.Hash
If ($DisplayProgessBar -eq $true){
Write-Progress -Activity ":: Removing items." `
-Status "[${ProcessedFileItemCounter} / ${numberOfItemsToRemove}]" `
-PercentComplete (($ProcessedFileItemCounter / $numberOfItemsToRemove) * 100) `
-Id 1 `
-ParentId 0
++$ProcessedFileItemCounter
}
If ($listOfFileHashToFilePath.ContainsKey($fileHash)) {
$Logger.Debug(" Found duplicated hash >>${fileHash}<<, removing >>${filePathToMatchingItem}<<.")
$FullQualifiedPath = Join-Path -Path $pathWithoutStarAtTheEnd -ChildPath $matchingItem
Remove-ItemAndLogResult "${FullQualifiedPath}" $true $Logger $isDryRun
++$numberOfRemovedFileSystemObjects
} Else {
$Logger.Debug(" Adding key >>${fileHash}<< with value >>${matchingItem}<<.")
$listOfFileHashToFilePath.Add($fileHash, $matchingItem)
}
} Else {
$Logger.Debug(" Filepath is not valid.")
}
}
} Else {
$Logger.Debug(" Less than two matching entries in the collection. Skipping duplicate check.")
}
}
}
Return $numberOfRemovedFileSystemObjects
}
Function Start-PathTruncations {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.Collections.ArrayList] $CollectionOfTruncableObjects,
[Parameter(Mandatory = $true)]
[Logger] $Logger,
[Parameter(Mandatory = $false)]
[bool] $beVerbose = $false
)
$ListOfUserPaths = Get-ChildItem "C:\Users" | Select-Object Name
$CurrentTruncableObjectCounter = 0
$DisplayProgressBar = ($beVerbose -ne $true)
$ListOfUserNames = $ListOfUserPaths.Name
$NumberOfRemovedFileSystemObjects = 0
$TotalAmountOfTruncableObjects = $CollectionOfTruncableObjects.Count
If ($DisplayProgressBar -eq $true) {
Clear-Host
}
ForEach ($CurrentTruncableObject In $CollectionOfTruncableObjects) {
$CurrentObjectPath = $CurrentTruncableObject.Get-Path()
#check if path ends with a wildcard
If ($CurrentObjectPath -match '\$user') {
ForEach ($currentUserName In $ListOfUserNames) {
$CurrentUserDirectoryPath = $CurrentObjectPath -replace '\$user', $currentUserName
If ($DisplayProgressBar -eq $true) {
Write-Progress -Activity ":: Processing list of truncable objects." `
-Status "[${CurrentTruncableObjectCounter} / ${TotalAmountOfTruncableObjects}]" `
-PercentComplete (($CurrentTruncableObjectCounter / $TotalAmountOfTruncableObjects) * 100) `
-CurrentOperation " Processing path >>${CurrentUserDirectoryPath}<<" `
-Id 0
}
$CurrentTruncableObject.Set-Path($CurrentUserDirectoryPath)
$NumberOfRemovedFileSystemObjects = Start-PathTruncation $CurrentTruncableObject $Logger $numberOfRemovedFileSystemObjects $isDryRun
}
} Else {
If ($DisplayProgressBar -eq $true) {
Write-Progress -Activity ":: Processing list of truncable objects." `
-Status "[${CurrentTruncableObjectCounter} / ${TotalAmountOfTruncableObjects}]" `
-PercentComplete (($CurrentTruncableObjectCounter / $TotalAmountOfTruncableObjects) * 100) `
-CurrentOperation " Processing path >>${CurrentObjectPath}<<" `
-Id 0
}
$NumberOfRemovedFileSystemObjects = Start-PathTruncation $CurrentTruncableObject $Logger $numberOfRemovedFileSystemObjects $isDryRun
}
++$CurrentTruncableObjectCounter
}
If ($DisplayProgressBar -eq $true) {
Clear-Host
}
Return $NumberOfRemovedFileSystemObjects
}
Start-CleanUpSystem