-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNNLogCleanerScript-RunBeforeBackup.ps1
97 lines (83 loc) · 3.74 KB
/
DNNLogCleanerScript-RunBeforeBackup.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
# Define the primary directory where the websites are located
$primaryDirectory = "C:\inetpub\wwwroot"
# Generate a unique log file name with a timestamp
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$logFileName = "log_cleanup_report_$timestamp.txt"
$logFilePath = Join-Path -Path $primaryDirectory -ChildPath $logFileName
# Function to log messages to both console and log file
function Log-Message {
param (
[string]$message
)
$timestampedMessage = "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") - $message"
Write-Output $timestampedMessage
Add-Content -Path $logFilePath -Value "$timestampedMessage`n"
}
# Function to safely get directories, handling errors
function Get-SafeDirectories {
param (
[string]$path
)
try {
Get-ChildItem -Path $path -Directory -ErrorAction Stop
} catch {
Log-Message "Failed to get directories in $($path): $($_.Exception.Message)"
return @()
}
}
# Check if script is in test mode
$testMode = $false
$totalFilesDeleted = 0
$totalSizeDeleted = 0
# Get directories to process
if ($testMode) {
$directoriesToProcess = Get-SafeDirectories -Path $primaryDirectory | Select-Object -First 3
} else {
$directoriesToProcess = Get-SafeDirectories -Path $primaryDirectory
}
# Log start of the process
Log-Message "Script started in $(if ($testMode) { 'test' } else { 'real' }) mode."
# Loop through each directory
foreach ($dir in $directoriesToProcess) {
$directoryFilesDeleted = 0
$directorySizeDeleted = 0
try {
$logsDirectory = Join-Path -Path $dir.FullName -ChildPath "Portals\_default\Logs"
$filesToDelete = Get-ChildItem -Path $logsDirectory -Filter "*.log.resources" -File -ErrorAction Stop
$filesToDeleteCount = $filesToDelete.Count
Log-Message "Processing directory: $($dir.Name). Files to delete: $filesToDeleteCount"
if ($filesToDeleteCount -eq 0) {
Log-Message "No log files found in $($dir.Name). Skipping."
continue
}
$fileIndex = 0
foreach ($file in $filesToDelete) {
if (-not $testMode) {
try {
Remove-Item -Path $file.FullName -Force
} catch {
Log-Message "Failed to delete file: $($file.FullName). Error: $($_.Exception.Message)"
continue
}
}
$fileSize = $file.Length
$directorySizeDeleted += $fileSize
$totalSizeDeleted += $fileSize
$directoryFilesDeleted++
$totalFilesDeleted++
$fileIndex++
$progressPercent = [Math]::Round(($fileIndex / $filesToDeleteCount) * 100, 2)
Write-Progress -Activity "Deleting log files in $($dir.Name)" -Status "$progressPercent% complete" -PercentComplete $progressPercent
Log-Message "Deleted: $($file.Name), Size: $([Math]::Round($fileSize / 1MB, 2)) MB"
}
Log-Message "Deleted $directoryFilesDeleted files from $($dir.FullName). Total size: $([Math]::Round($directorySizeDeleted / 1MB, 2)) MB. Logs directory: $logsDirectory"
} catch {
Log-Message "Error processing $($dir.FullName): $($_.Exception.Message)"
}
}
# Log the total number of files and size deleted
Log-Message "Total files deleted: $totalFilesDeleted"
Log-Message "Total size deleted: $([Math]::Round($totalSizeDeleted / 1MB, 2)) MB"
Log-Message "Script execution completed. Please check the log file for details: $logFilePath"
# Display completion message
Write-Host "Script completed. Total files deleted: $totalFilesDeleted. Total size deleted: $([Math]::Round($totalSizeDeleted / 1MB, 2)) MB. Log file: $logFilePath"