Log Manager Utility v1.8

In my environment, I need to keep an archive of some log files for a period of time, whereas other logs are not so critical, and some applications generate logs daily which are already compressed into ZIP files. So I have these various requirements for log files. I don’t want them filling up my server hard drives and I don’t want to disable logging, so I wrote this script that could cater to these requirements.

This utility is available to download from the Microsoft TechNet GalleryPowerShell Gallery and GitHub.

I take great care to test my ideas and make sure my articles are accurate before posting, however mistakes do slip through sometimes. If you’d like to get in touch with me please use the comments, Twitter (you can tweet me and my DMs are open) or my contact form.

I hope this article helps you out, please consider supporting my work here. Thank you.

-Mike

 

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.

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, various firewall logs, Minecraft Server logs, IRC client logs and the logs generated by my own utilities.

 

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.

 

Configuration

The table below shows all the command line options available with descriptions and example configurations.

Command Line Switch Mandatory Description Example
-Path Yes The location of the log files you wish to manage. C:\inetpub\logs\LogFiles\W3SVC*\*

OR

\\server\share$\Logs

-Days Yes Entering 30 will mean that files older than 30 days will be managed. Any number greater than 0

30

-Backup No* The location to copy the files to. *This switch is not mandatory, but must be used if a zip file is to be created. C:\Log-Archive

OR

\\server\archive

-WorkDir No Specify the working directory for the ZIP file to be created. Ideally it should be local to the server for best performance. C:\foo
-L No Location to store the optional log file. The name of the log file is automatically generated. C:\foo
-Subject No The subject line that the email should have. Encapsulate with single or double quotes. ‘Server: Notification’
-SendTo No The email address to send the log file to. me@contoso.com
-From No* The email address that the log file should be sent from.

*This switch isn’t mandatory but is required if you wish to email the log file.

example@contoso.com
-Smtp No* SMTP server address to use for the email functionality.

*This switch isn’t mandatory but is required if you wish to email the log file.

mail01.contoso.com

OR

smtp.live.com

OR

smtp.office365.com

-User No* The username of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

example@contoso.com
-Pwd No* The location of the file containing the encrypted password of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on your SMTP server.

c:\foo\ps-script-pwd.txt
-UseSsl No* Add this option if you wish to use SSL with the configured SMTP server.

Tip: If you wish to send email to outlook.com or office365.com you will need this.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

N/A

 

Change Log

2019-09-04 v1.8

  • Added custom subject line for e-mail.

2017-10-16 v1.7

  • Changed SMTP authentication to require an encrypted password file.
  • Added instructions on how to generate an encrypted password file.

2017-10-09 v1.6

  • Added necessary information to add the script to the PowerShell Gallery.

2017-09-20 v1.5

  • Improved log output to be more legible.

2017-06-21 v1.4

  • Improved commenting on the code for documentation purposes.
  • Added authentication and SSL options for e-mail notification.

2017-06-13 v1.3

  • First public release.

 

4 thoughts on “Log Manager Utility v1.8

Add yours

  1. Hi Mike. Thanks for sharing that. Worked on pretty much the first try with email and full use of the parameters. One question, is there a debug or “noop” mode? That would be useful.

    Like

    1. Hi Stuart,

      Thanks, I’m glad you’ve found it useful. I hadn’t considered adding a debug mode but, maybe in a future version. I tend to revisit my scripts from time to time and add new features, tweak them etc, as I learn more.

      Thanks again,
      Mike

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: