Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Uses the "not PSIsContainer" to ensure only files are removed, and not subdirectories. Change the location and the log location (or remove entirely)
- $DeleteOldFiles = get-childitem C:\temp -recurse | where {$_.lastwritetime -lt (get-date).adddays(-8) -and -not $_.psiscontainer} | ForEach-Object {remove-item $_.fullname -force}
- $DeleteOldfiles
- SendMail
- function SendMail
- {
- #Write-Host "Sending Email"
- #SMTP server name
- $smtpServer = "<SMTP SERVER NAME OR ADDRESS>"
- #Creating a Mail object
- $msg = new-object Net.Mail.MailMessage
- #Creating SMTP server object
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- $Content = Get-Content C:\temp\logs.txt
- $Body = Write-Host $Content
- #Email structure
- $msg.From = "Email Address"
- $msg.ReplyTo = "Email Address"
- $msg.To.Add("Email Address")
- $msg.subject = "File Deleted"
- $msg.body = $Body
- #Sending email
- $smtp.Send($msg)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement