Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Deletes unwanted files from sever
- .DESCRIPTION
- Deletes all .zip files that are older than 15 days from the location specified in the $Path variable and emails a file containing all deleted items
- .PARAMETERS
- .EXAMPLE
- #>
- $Path = "\\[SERVERNAME]\c$\Program Files\Software\Backup"
- $Date = (Get-date).AddDays(-15)
- $Files = Get-ChildItem -path $Path -include *.zip -recurse | where { ($_.CreationTime) -le $Date}
- $SMTPServer="mailserver.domain.com"
- $logFile = "c:\temp\FilesDeleted.txt"
- $testing = "Disabled" # Set to Disabled to Email Users
- #Outputs a file that contains the logs that are to be deleted
- $Files | Out-File "c:\temp\FilesDeleted.txt"
- #Performs the delete of the files
- $Files | Remove-Item -Force
- #Emails $Recipient with the log file attached
- # If Testing Is Enabled - Email Administrator
- if (($testing) -eq "Enabled")
- {
- $Recipient = $testRecipient
- } # End Testing
- # Email Subject Set Here
- $subject="Backups were deleted"
- # Email Body Set Here, Note You can use HTML, including Images.
- $body ="<font face=Arial>
- Hello,
- <p>The backups that are 15 days or older were successfully deleted<br>
- <p>Thanks, <br>
- <p> The Ghost in the machine!<br>
- </font>
- </P>"
- # Send Email Message
- Send-Mailmessage -smtpServer $smtpServer -from $from -to $Recipient -subject $subject -body $body -Attachments $logfile -bodyasHTML -priority High
Advertisement
Add Comment
Please, Sign In to add comment