PowerShell based log file cleanup/archiver
For full instructions and documentation, visit my blog post
Please consider donating to support my work:
- You can support me on a monthly basis using Patreon.
- You can support me with a one-time payment using PayPal or by using Kofi.
Log Manager can also be downloaded from:
Tweet me if you have questions: @mikegalvin_
Features and Requirements
This utility can be configured to do the following:
- Delete files and folder trees older than X days.
- Move files and folder trees to another location.
- Archive files and folder trees as a ZIP file.
A Quick Self-Aware Note
I’m totally aware that my script is designed to manage log files and yet it will also generate a log file, if required. I prefer to get a notification when a server runs a script, so that’s why all of my scripts have this option. Some of my scripts generate a log file that gets overwritten each time they run, so they shouldn’t build up over time, but for everything else this script should help.
Generating A Password File
The password used for SMTP server authentication must be in an encrypted text file. To generate the password file, run the following command in PowerShell, on the computer that is going to run the script and logged in with the user that will be running the script. When you run the command you will be prompted for a username and password. Enter the username and password you want to use to authenticate to your SMTP server.
Please note: This is only required if you need to authenticate to the SMTP server when send the log via e-mail.
$creds = Get-Credential $creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt
After running the commands, you will have a text file containing the encrypted password. When configuring the -Pwd switch enter the path and file name of this file.
Here’s a list of all the command line switches and example configurations.
The root path that contains the files and or folders that the script should operate on.
The number of days from the current date that files created during should be untouched.
The location that the files and folders should be backed up to. If you do not set this, a back up will not be performed.
The path of the working directory used for ZIP file creation. This should be local for best performance. If you do not set this, the files will not be zipped.
The path to output the log file to. The file name will be Log-Manager.log.
The email subject that the email should have. Encapulate with single or double quotes.
The e-mail address the log should be sent to.
The from address the log should be sent from.
The DNS name or IP address of the SMTP server.
The user account to connect to the SMTP server.
The password for the user account.
Connect to the SMTP server using SSL.
Log-Manager.ps1 -Path C:\inetpub\logs\LogFiles\W3SVC*\* -Days 30 -Backup \\nas\archive -WorkDir C:\scripts -L C:\scripts\logs -Subject 'Server: Logs Cleanup' -SendTo email@example.com -From Log-Manager@contoso.com -Smtp exch01.contoso.com -User firstname.lastname@example.org -Pwd P@ssw0rd -UseSsl
With these settings, the script will archive IIS logs files older than 30 days as a ZIP file in \nas\archive, using the C:\scripts folder as a working directory. The log file of the scritp will be output to C:\scripts\logs and e-mailed with a custom subject line, using an SSL connection.