Overview
Depending on your needs you can configure this script 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.
Compatibility and Requirements
The script has been tested on Windows 10, Windows Server 2016, Windows Server 2012 R2, and Windows Server 2008 R2. It has been used to manage Microsoft Deployment Toolkit logs, IIS log files, firewall logs, Minecraft Server logs, IRC client logs and the logs generated by my own scripts posted about here.
The PowerShell -file Parameter
When running the script via Schedule Tasks or the command prompt be sure to use the -file parameter before specifying the script, so you can use “double quotes” for the options that need them, if you do not use -file, then you should use ‘single quotes’.
Command Line Options
Here are the command line options available to you, with some examples of how to use them.
Command Line Switch | Mandatory | Description | Example |
-path | Yes | The location of the log files you wish to manage. | C:\inetpub\logs\LogFiles\W3SVC*\*OR
\\mdt01\deploymentshare$\Logs |
-days | Yes | Entering 30 will mean that files older than 30 days will be managed. | Any number greater than 030 |
-backup | No* | The location to copy the files to. *This switch is optional but must be set if a zip file is to be created. | F:\Log-ArchiveOR
\\nas\archive |
-workdir | No | Specify the location for the ZIP file to be created. Ideally it should be local on the server for best performance. | E:\scripts |
-l | No | Location to store the optional log file. The name of the log file is automatically generated. | E:\scripts\log |
-sendto | No* | The email address to send the log file to.*Log file switch must be set, as well as other mail config options. | me@contoso.com |
-from | No* | The email address that the log file should be sent from.*Log file switch must be set, as well as other mail config options. | Log-Man@contoso.com |
-smtp | No* | Mail server to use to send the log file.*Log file switch must be set, as well as other mail config options. | mail01.contoso.comOR
smtp.live.com OR smtp.office365.com |
-user | No* | The username of the account to use for email auth.*This switch is not required, for the script, but maybe required depending on the smtp server.
Tip: If you wish to send email to outlook.com or office365.com you will need the -usessl switch. |
example@contoso.com |
-pwd | No* | The password of the account to use for email auth.*This switch is not required, for the script, but maybe required depending on the smtp server. | P@ssw0rd |
-usessl | No* | Use this switch if you wish to send email via an ssl connection.*You must have also set a username and password to use this switch. | N/A |
Self aware note: I’m totally aware that my script is designed to manage logs and yet it will also generate a log, if required. I really like to get a notification when a server runs a script, so that’s why all of my scripts have this option. Some of my recent 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.
The Complete Script
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 |
# ------------------------------------------- # Script: log-manager_v1-4.ps1 # Version: 1.4 # Author: Mike Galvin twitter.com/digressive # Date: 21/06/2017 # ------------------------------------------- ##Set up command line switches and what variables they map to Param( [parameter(Mandatory=$true)] [alias("path")] $worklogs, [alias("workdir")] $zip, [parameter(Mandatory=$true)] [alias("days")] $time, [alias("backup")] $dest, [alias("l")] $logpath, [alias("sendto")] $mailto, [alias("from")] $mailfrom, [alias("smtp")] $smtpserver, [alias("user")] $smtpuser, [alias("pwd")] $smtppwd, [switch]$usessl) ##Count the number of files that are old enough to work on in the configured directory $fileno = Get-ChildItem $worklogs –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-$time) | Measure-Object ##If the number of the files to work on is zero, do nothing If ($fileno.count -ne 0) { ##If logging is configured, set up variables and start logging If ($logpath) { $logfile = "cleanup-logs.log" $log = "$logpath\$logfile" Start-Transcript $log } ##If the zip option was configured, copy the working files to a temp dir and create a zip file, then remove the temp dir and move the zip file If ($zip) { Add-Type -AssemblyName "system.io.compression.filesystem" New-Item -Path $zip\temp -ItemType Directory Get-ChildItem $worklogs | Where-Object CreationTime –lt (Get-Date).AddDays(-$time) | Copy-Item -Destination $zip\temp -Recurse -Force -Verbose [io.compression.zipfile]::CreateFromDirectory("$zip\temp", "$zip\Logs-{0:yyyy-MM-dd-HH-mm}.zip" -f (Get-Date)) Remove-Item $zip\temp -Recurse -Verbose Move-Item $zip\Logs-*.zip $dest -Verbose Get-ChildItem $worklogs –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-$time) | Remove-Item -Recurse -Verbose } ##If the backup directory was configured, copy the files to the backup dir If ($dest) { Get-ChildItem $worklogs | Where-Object CreationTime –lt (Get-Date).AddDays(-$time) | Copy-Item -Destination $dest -Recurse -Force -Verbose } ##If no backup options were configured, or after doing the previous operations, remove the old files Get-ChildItem $worklogs –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-$time) | Remove-Item -Recurse -Verbose ##If log was configured then stop the log If ($logpath) { Stop-Transcript ##If email was configured then set up variables for sending the log If ($smtpserver) { $mailsubject = "Log Manager Log" $mailbody = Get-Content -Path $log | Out-String ##If an smtp password was configured, create a variable with the username and password If ($smtppwd) { $smtpcreds = New-Object System.Management.Automation.PSCredential -ArgumentList $smtpuser, $($smtppwd | ConvertTo-SecureString -AsPlainText -Force) ##If ssl was configured, use it and send the log If ($usessl) { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -UseSsl -Credential $smtpcreds } ##If ssl wasn't configured, don't use it and send the log Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -Credential $smtpcreds } } ##If an smtp password wasn't configured, send the log without authentication Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver } } } } ##End |
No Comments