-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurging.ps1
132 lines (113 loc) · 4.54 KB
/
Purging.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
# Set the default reference directory
$rootdir = "E:\"
#path for the Purge files
[string]$sourcepath = $args[0]
##path for the Purge folders
[string]$processingpath = $args[1]
# Day count to delete the files
$daycount = $args[2]
# Return value for success and failure
$returnvalue
##Tendays old file Date setup
[string]$olderfiledaycount = (Get-Date).AddDays($daycount)
# Set the Log File Path
$logpath = "$($rootdir)\Purging\Logging\Purge\"
# Set the current Date
$CurrentDate = (Get-Date)
# Converting the date to string
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
# Set the log file path
$logfilepath = "$($logpath)Log_$($CurrentDate).log"
Write-Output "********Files/Folders Deletion Log***********" >> $logfilepath
#To get the measure of the file
$filecount = Get-ChildItem $sourcepath -Recurse | Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -lt $olderfiledaycount}
$measurefiles = $filecount | Measure-Object
#Purge the older files if the files are greater than zero
if(($measurefiles).count -gt 0 -and $sourcepath -ne '')
{
try {
Write-Output "------------------------------" >> $logfilepath
Write-Output "$(get-date) - File Deletion started" >> $logfilepath
Write-Output "------------------------------" >> $logfilepath
$Files = Get-ChildItem $sourcepath -Recurse | Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -lt $olderfiledaycount} |
ForEach-Object {
$file = $_
Remove-Item -Recurse $file.fullname -ErrorAction Stop
$returnvalue = +1
$lasttimefile = $file.LastWriteTime
$filelog = $file | select -Expand FullName
Write-Output "Deleted Files: $($filelog) $($lasttimefile)" >> $logfilepath
}
} catch {
$returnvalue = -1
Add-Content $logfilepath "`nError deleting file: $file, $_"
Write-Output "------------------------------" >> $logfilepath
Write-Output "Exception:File Deletion:" >> $logfilepath
Write-Output "------------------------------" >> $logfilepath
Add-Content $logfilepath "`nError deleting file path: $file, $_"
$ErrorMessage = $_.Exception.Message >> $logfilepath
$FailedItem = $_.Exception.ItemName >> $logfilepath
Write-Output $($CurrentDate) >> $logfilepath
Write-Output "************************`r`n`r`n" >> $logfilepath
}
}else{
if(($measurefiles).count -eq 0 -and $sourcepath -ne ''){
Write-Output "$(get-date) - File Deletion started " >> $logfilepath
Write-Output "No files found" >> $logfilepath
$returnvalue = 0
}
else{}
}
$foldercount = Get-ChildItem $processingpath -Recurse | Where-Object {$_.PSIsContainer -and $_.Name -like "20*" -and $_.LastWriteTime -lt $olderfiledaycount}
$measurefolders = $foldercount | Measure-Object
#Purge the older folders if the folders are greater than zero
if(($measurefolders).count -gt 0 -and $processingpath -ne '')
{
try {
Write-Output "------------------------------" >> $logfilepath
Write-Output "$(get-date) - Folder Deletion started" >> $logfilepath
Write-Output "------------------------------" >> $logfilepath
$Folders = Get-ChildItem $processingpath -Recurse | Where-Object {$_.PSIsContainer -and $_.Name -like "20*" -and $_.LastWriteTime -lt $olderfiledaycount} |
ForEach-Object {
$folder = $_
Remove-Item -Recurse $folder.fullname -ErrorAction Stop
$returnvalue = +1
$lasttimefolder = $folder.LastWriteTime
$folderlog = $folder | select -Expand FullName
Write-Output "Deleted Folders: $($folderlog) $($lasttimefolder)" >> $logfilepath
} }catch {
$returnvalue = -1
Write-Output "------------------------------" >> $logfilepath
Write-Output "Exception:Folder Deletion:" >> $logfilepath
Write-Output "------------------------------" >> $logfilepath
Add-Content $logfilepath "`nError deleting folder path: $folder, $_"
$ErrorMessage = $_.Exception.Message >> $logfilepath
$FailedItem = $_.Exception.ItemName >> $logfilepath
Write-Output $($CurrentDate) >> $logfilepath
Write-Output "************************`r`n`r`n" >> $logfilepath
}
}
else
{
if(($measurefolders).count -eq 0 -and $processingpath -ne ''){
Write-Output "$(get-date) - File Deletion started" >> $logfilepath
Write-Output "No folders found" >> $logfilepath
$returnvalue = 0
}
else
{}
}
#sucess/Failure log
Write-Output "*******************" >> $logfilepath
if($returnvalue -eq 1){
Write-Output "$(get-date) - Files/Folders are deleted successfully" >> $logfilepath
}
elseif($returnvalue -eq -1){
Write-Output "$(get-date) - Failed to delete the $daycount older Files/Folders" >> $logfilepath
}
#job suceess/failure return message
if ($returnvalue -eq -1) {
exit $returnvalue
} else {
exit 0
}