-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogCleanTask.ps1
121 lines (109 loc) · 4.14 KB
/
LogCleanTask.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
# LogCleanTask.ps1 v1.0 (c) Chris Redit
## Settings
# The commented variables below are used to configure the script.
# Please see https://github.com/chrisred/posh-scripts#logcleantaskps1 for the full README.
# The folders to delete files from, this includes the retention period in days, and the path to the folder
$Paths = @(
@{'Retention' = 7; 'Path' = 'C:\Example1'};
@{'Retention' = 14; 'Path' = '\\HOST1\c$\Example2'};
)
# Set the -Force parameter value, $true will allow hidden and read-only files to be deleted, default $false
$ForceRemove = $false
# Set the -WhatIf parameter value, $true will only log actions no files will be deleted, default $false
$WhatIf = $false
# Enable the log file (for event log logging a source named 'LogClean' must be registered), default $false
$EnableLogFile = $false
# Path to write the log file to, default is 'C:\Windows\Temp'
$LogPath = "$env:WINDIR\Temp"
$LogTime = Get-Date -Format 'yyMMddHHmm'
Function Write-Log ([String]$Message, [Nullable[Int]]$EventId = $null, [String]$EventType = 'Information')
{
if ($EnableLogFile)
{
if (-not (Test-Path -Path $LogPath)) {New-Item -ItemType Directory -Path $LogPath}
Out-File -InputObject $Message -FilePath (Join-Path -Path $LogPath -ChildPath "logclean-$($LogTime).log") -Append
}
try
{
if ($EventId -ne $null)
{
Write-EventLog -LogName Application -Source 'LogClean' -Message $Message -EventId $EventId -EntryType $EventType -EA Stop
}
}
catch
{
Write-Warning "Event log: $($_.ToString())"
}
}
try
{
$Events = [System.Collections.ArrayList]@()
$TotalRemoveCount = 0
foreach ($Path in $Paths)
{
$FileRemoveCount = 0
$FileErrorCount = 0
$TotalFileSize = 0
# track events for each iteration, $i returns the index of the new item
$i = $Events.Add(@{
'Retention' = $Path.Retention;
'Message' = 'None';
})
if (Test-Path -LiteralPath $Path.Path)
{
$RemoveTime = (Get-Date).AddDays(-$Path.Retention)
$Files = @(Get-ChildItem -LiteralPath $Path.Path | Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -lt $RemoveTime})
foreach ($File in $Files)
{
Remove-Item -LiteralPath $File.FullName -Force:$ForceRemove -WhatIf:$WhatIf
if ($?)
{
$FileRemoveCount++
$TotalFileSize += $File.Length
}
else
{
$FileErrorCount++
Write-Warning "Error removing $($File.Name)"
}
}
$TotalRemoveCount += $FileRemoveCount
$Events[$i]['Files'] = $FileRemoveCount
$Events[$i]['Errors'] = $FileErrorCount
$Events[$i]['Size'] = $TotalFileSize
$Events[$i]['Path'] = $Path.Path
$events[$i]['Message'] = 'Path found.'
}
else
{
$Events[$i]['Files'] = 'N/A'
$Events[$i]['Errors'] = 'N/A'
$Events[$i]['Size'] = 'N/A'
$Events[$i]['Path'] = $Path.Path
$events[$i]['Message'] = 'Path does not exist.'
}
}
}
catch
{
# write a short version of the exception details to the log and rethrow
$ErrorText += $_.ToString()+"`r`n"
$ErrorText += $_.InvocationInfo.PositionMessage+"`r`n"
Write-Log $ErrorText -EventId 1945 -EventType 'Error'
throw
}
finally
{
$EventText = $Events | ForEach-Object {' '+$_.Retention+' days, '+$_.Files+', '+$_.Errors+', '+$_.Size+' bytes, '+$_.Path+', '+$_.Message} | Out-String
$ErrorText = $Error | ForEach-Object {' '+$_.ToString()} | Out-String
$LogText = `
@"
$TotalRemoveCount file(s) removed from $($Paths.Count) folder(s):
Retention, Removed Files, File Errors, Total Size, Path, Message
$EventText
$($Error.Count) error(s) were encountered:
$ErrorText
"@
Write-Host $LogText
Write-Log $LogText -EventId 1946 -EventType 'Information'
}