-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemove-LogFile.ps1
More file actions
93 lines (76 loc) · 2.84 KB
/
Copy pathRemove-LogFile.ps1
File metadata and controls
93 lines (76 loc) · 2.84 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
<#PSScriptInfo
.VERSION 1.0.0
.GUID a1ba81d3-e859-4998-b6ff-c9b8413c7316
.AUTHOR @torggler
.TAGS Logs
.PROJECTURI https://ntsystems.it/PowerShell/Remove-LogFile/
#>
<#
.SYNOPSIS
Deletes log files.
.DESCRIPTION
Deletes log files, parameters can be used to specify the root folder, whether or not to include subfolders, a file extension filter and the age.
This is intended to be run as scheduled task to regularly clean-up log files.
.EXAMPLE
PS C:\> .\Remove-LogFile.ps1 -Path C:\inetpub\logs -Age 7 -Recurse
This example removes all *.log files older than 7 days from C:\inetpub\logs and any subfolders.
.INPUTS
None.
.OUTPUTS
None.
.NOTES
Author: Thomas Torggler; @torggler
Date: 2014-04-30
Version: 1.1
1.0: Basic Script
1.1: handle if no files to delete.
If PowerShell Version 2.0 is used, remove the -file Parameter from Get-Childitem
.LINK
https://ntsystems.it/PowerShell/Remove-LogFile/
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Specify folder in which logs will be deleted
[Parameter(Mandatory=$true,
Position=0)]
[ValidateScript({Test-Path $_ -PathType Container})]
$Path,
# Specify a number of days. Files with a LastWriteTime older than this will be deleted.
[Parameter(Mandatory=$false,
Position=1)]
[ValidateNotNullorEmpty()]
[ValidateRange(1,65535)]
[int]$Age = 90,
# Specify file extension filter. Defaults to '*.log'.
[Parameter(Mandatory=$false,
Position=2)]
[ValidatePattern('^\*\.\w{3}$')]
[string]
$Filter = '*.log',
# Specify a path to a log file. The script will log information and erros to the file.
[Parameter(Mandatory=$false)]
[System.IO.FileInfo]
$LogFile="$env:temp\log-RemoveLogFile.ps1",
# Include subfolders.
[switch]
$Recurse
)
# Clean and initialize Log File
Remove-Item $LogFile -ErrorAction SilentlyContinue -WhatIf:$false
"$(Get-Date) Remove-LogFile started!" | Add-Content $LogFile -WhatIf:$false
Write-Host "LogFile: $LogFile" -ForegroundColor Yellow
$filesToDelete = Get-ChildItem -Path:$Path -File -Recurse:$Recurse -Filter:$Filter | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-$Age)}
if ($filesToDelete) {
"$(Get-Date) Found $($filesToDelete.Count) files to delete." | Add-Content $LogFile
foreach ($file in $filesToDelete) {
if ($pscmdlet.ShouldProcess("$($File.FullName)", "Delete"))
{
Remove-Item -Path $file.FullName
"$(Get-Date) Removed $($file.FullName)" | Add-Content $LogFile
}
}
} else {
"$(Get-Date) Nothing to delete." | Add-Content $LogFile -WhatIf:$false
}
"$(Get-Date) Remove-LogFile ended!" | Add-Content $LogFile -WhatIf:$false