-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanupFunctions.ps1
156 lines (145 loc) · 4.09 KB
/
cleanupFunctions.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
# This function does a cleanup to delete files older than 7 days. It also keeps a weekly backup indicated by $weekOfBackup, which defaults to Friday.
# It then clears the weekly backups that are older than 30 days.
function Invoke-WeeklyCleanup
(
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$false)]
[string]$weekOfBackup = "Friday"
)
{
$week = (get-date).DayOfWeek
$date = get-date -format "yyyy_MM_dd"
$limit = (Get-Date).AddDays(-7)
$limit_wk = (Get-Date).AddDays(-30)
$path_wk = $path + '\weekly'
try {
if (Test-Path $path) {
if ($week -eq $weekOfBackup) {
$allDailyBackupFiles = Get-ChildItem -Path $path |?{!$_.PSIsContainer}
$latestBackupFile = $allDailyBackupFiles | Sort-Object -Property CreationTime -Descending | Select-Object -First 1
$file = $latestBackupFile.FullName
if (Test-Path $file)
{
if (Test-Path $path_wk) {
Move-Item -path $file -destination $path_wk
}
else {
throw "Weekly destination path does not exist"
}
}
else {
throw "Weekly backup does not exist"
}
}
# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Get-ChildItem -Path $path_wk -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit_wk } | Remove-Item -Force
}
else {
throw "Path doesn't exist"
}
}
catch [Exception] {
throw "File clean up failed"
}
}
# This function does a cleanup of files indicated in the $path parameter are older than $days, which is currently defaulted at 7.
function Invoke-Cleanup (
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$false)]
[int]$days = 7
)
{
$days = -1*$days
$limit = (Get-Date).AddDays($days)
try {
if (Test-Path $path) {
# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
}
else {
throw "Path doesn't exist"
}
}
catch [Exception] {
throw "File clean up failed"
}
}
function Invoke-Archive
(
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$false)]
[int]$days = 30,
[Parameter(Mandatory=$false)]
[int]$days_arc = 600
)
{
$days = -1*$days
$days_arc = -1*$days_arc
$week = (get-date).DayOfWeek
$date = get-date -format "yyyy_MM_dd"
$limit = (Get-Date).AddDays($days)
$limit_arc = (Get-Date).AddDays($days_arc)
$path_arc = $path + '\archive'
try {
if (Test-Path $path) {
if (Test-Path $path_arc) {
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Move-Item -destination $path_arc
}
else {
mkdir $path_arc
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Move-Item -destination $path_arc
}
# Delete files older than the $limit_arc
Get-ChildItem -Path $path_arc -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit_arc } | Remove-Item -Force
}
else {
throw "Path doesn't exist"
}
}
catch [Exception] {
throw "File clean up failed"
}
}
function Invoke-Removal
(
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$false)]
[string]$exclude,
[Parameter(Mandatory=$false)]
[int]$days = 30
)
{
$days = -1*$days
$week = (get-date).DayOfWeek
$date = get-date -format "yyyy_MM_dd"
$limit = (Get-Date).AddDays($days)
try {
if (Test-Path $path)
{
Get-ChildItem -Path $path -Exclude $exclude -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
try {
$directories = Get-ChildItem -Path $path -Directory -Recurse -Force | Select-Object FullName
foreach ($directory in $directories) {
$count = Get-ChildItem -Path $directory.FullName | Measure-Object
if ($count.count -eq 0) {
Remove-Item -Path $directory.FullName -Recurse -Force
}
}
}
catch [Exception] {
throw "Empty directory removal failed"
}
}
else {
throw "Path doesn't exist"
}
}
catch [Exception] {
throw "File clean up failed"
}
}