-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRemove-OldFiles.ps1
138 lines (115 loc) · 4.38 KB
/
Remove-OldFiles.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
<#
.SYNOPSIS
Checks all files in a specified path for files older than the specified amount of days, then deletes them and emails a report.
.DESCRIPTION
Checks all files in a specified path for files older than the specified amount of days, then deletes them and emails a report.
.PARAMETER Path
Folder path. Should be enclosed in quotation marks.
.PARAMETER MaxAge
Number of days after which as file should be deleted.
.PARAMETER MailServer
SMTP server address
.PARAMETER RecipientAddress
Recipient email
.PARAMETER SenderAddress
Sender email address
.PARAMETER MailCredential
Credentials for mailserver for sender address. If none are specified anonymous credentials will be used.
.EXAMPLE
Remove-OldFiles -Path "C:\temp\" -MaxAge 30 -mailserver mail.server.com -recipientaddress "[email protected]" -senderaddress "[email protected]"
.NOTES
#>
function Remove-OldFiles {
param(
$Path,
$MaxAge,
$mailserver,
$recipientaddress,
$senderaddress,
$mailcredential
)
$css = 'h1 {
margin-left: auto;
margin-right: auto;
text-transform: uppercase;
text-align: center;
font-size: 40pt;
font-weight: bold;
}
body {
margin-left: auto;
margin-right: auto;
text-align: center;
font-family: "Segoe UI";
font-weight: lighter;
font-size: 10pt;
color:#2f2f2f;
background-color: white;
}
table {
margin-left: auto;
margin-right: auto;
border-width: 1px;
border-style: solid;
border-color: #2f2f2f;
border-collapse: collapse;
}
th {
font-family: "Segoe UI";
font-weight: lighter;
color: white;
text-transform: capitalize;
margin-left: auto;
margin-right: auto;
border-width: 1px;
border-style: solid;
border-color: #2f2f2f;
background-color: #d32f2f;
}
td {
margin-left: auto;
margin-right: auto;
border-width: 1px;
border-style: solid;
border-color: #2f2f2f;
background-color: white;
}
'
$server = $env:computername
$files = get-childitem -recurse $path | where {$_.LastWriteTime -lt ((get-date).adddays(-$MaxAge))}
if ($files -ne $null){
$files | where {$_.PSIsContainer -ne $true} | % {remove-item $_.FullName -Confirm:$false}
$html = convertto-html -head "<style>$css</style>" -Title "Deleted Files for $path" -body "<h1>Deleted Files for $path on $server</h1>"
$html += $files | where {$_.PSIsContainer -ne $true} | select FullName,LastWriteTime | convertto-html}
ELSE {$html = convertto-html -Body "No Files older than $maxage days"}
$html | out-file $env:TEMP\1.html
[string]$html = Get-Content $env:TEMP\1.html
#Setup Anonymous credentials to pass to mail cmdlet.
$pass = ConvertTo-SecureString "whatever" -asplaintext -force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $pass
Send-Mail -senderaddress $senderaddress -recipientaddress $recipientaddress -mailserver $MailServer -Subject "File Deletions for $path" -bodyashtml $html -port 25 -mailcredential $creds
}
function Send-Mail {
[CmdletBinding()]
param(
$body,
[Parameter(ValueFromPipeline=$True)]$bodyashtml,
$mailserver,
$recipientaddress,
$senderaddress,
$subject,
$port,
$mailcredential
)
if(!($mailcredential)){
$pass = ConvertTo-SecureString "whatever" -asplaintext -force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $pass
}
ELSE {$creds = $mailcredential}
if ($bodyashtml){
send-mailmessage -From $senderaddress -To $recipientaddress -SmtpServer $MailServer -Subject $subject -bodyashtml $bodyashtml -port $port -Credential $creds
}
else {
send-mailmessage -From $senderaddress -To $recipientaddress -SmtpServer $MailServer -Subject $subject -body $body -port $port -Credential $creds
}
}