Log Manager Utility

Flexible Clean Up and Backup of Log Files

Log Manager Utility can also be downloaded from:

Please consider supporting my work:

  • Sign up using Patreon.
  • Support with a one-time payment using PayPal.

If you’d like to get in touch with me please leave a comment, send me a tweet or DM, or send me a message via my contact form.

-Mike

 

Features and Requirements

  • The utility will delete files and folders older than X days.
  • Can also backup files and folders older than X days to another location.
  • Backup files and folders as a zip file.
  • The utility requires at least PowerShell 5.0.
  • This utility has been tested on Windows 10, Windows Server 2019, Windows Server 2016 and Windows Server 2012 R2 (Datacenter and Core Installations) with PowerShell 5.0.

 

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 and logged in with the user that will be running the utility. 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 Description Example
-LogsPath The path that contains the logs that the utility should process. C:\inetpub\logs\LogFiles\W3SVC**
-LogKeep Instructs the utility to keep a specified number of days’ worth of logs. Logs older than the number of days specified will be deleted. 30
-BackupTo The path the logs should be backed up to. A folder will be created inside this location. Do not add a trailing backslash. If this option is not used, backup will not be performed. \\nas\archive
-BacKeep Instructs the utility to keep a specified number of days’ worth of backups. Backups older than the number of days specified will be deleted. Only backup folders or zip files created by this utility will be removed. 30
-Wd The path to the working directory to use for the backup before copying it to the final backup directory. Use a directory on local fast media to improve performance. C:\temp
-Compress This option will create a zip file of the log files. N/A
-Sz Configure the utility to use 7-Zip to compress the log files. 7-Zip must be installed in the default location ($env:ProgramFiles) if it is not found, Windows compression will be used as a fallback. N/A
-ZipName Enter the name of the zip file you wish to have. If the name includes a space, encapsulate with single quotes. The time and date will be appended to this name. If this option is not used, a default name of logs-HOSTNAME-date-time.zip will be used. ‘IIS Logs’
-NoBanner Use this option to hide the ASCII art title in the console. N/A
-L The path to output the log file to. The file name will be Log-Man_YYYY-MM-dd_HH-mm-ss.log. Do not add a trailing \ backslash. C:\scripts\logs
-Subject The subject line for the e-mail log. Encapsulate with single or double quotes. If no subject is specified, the default of “Log Manager Utility Log” will be used. ‘Server: Notification’
-SendTo The e-mail address the log should be sent to. me@contoso.com
-From The e-mail address the log should be sent from. LogMan@contoso.com
-Smtp The DNS name or IP address of the SMTP server. smtp.live.com OR smtp.office365.com
-User The user account to authenticate to the SMTP server. example@contoso.com
-Pwd The txt file containing the encrypted password for SMTP authentication. C:\scripts\ps-script-pwd.txt
-UseSsl Configures the utility to connect to the SMTP server using SSL. N/A

 

Example

Log-Manager.ps1 -LogPath C:\inetpub\logs\LogFiles\W3SVC*\*
-LogKeep 30 -BackupTo \\nas\archive -BacKeep 30 -Wd C:\temp
-Compress -L C:\scripts\logs -Subject 'Server: Log Manager'
-SendTo me@contoso.com -From Log-Manager@contoso.com
-Smtp smtp.outlook.com -User me@contoso.com
-Pwd C:\foo\pwd.txt -UseSsl

The above command will backup and remove IIS logs older than 30 days. It will create a zip folder using the C:\temp folder as a working directory and the file will be stored in \\nas\archive. The log file will be output to C:\scripts\logs and sent via e-mail with a custom subject line.

 

Change Log

2020-03-23: Version 20.03.23

  • Fixed a user reported issue: wildcards in the -LogsPath switch no longer worked. They will now work.

2020-03-11: Version 20.03.11 ‘Cow’

  • The previous command line switch of -LogPath is now -LogsPath. This fixes an issue with a duplicate variable and switch alias preventing utility from running currently.

2020-03-09: Version 20.03.06 ‘Fish’

  • Added custom name for zip file based on user feedback.
  • Added option to use 7-zip for zip file backup.
  • Added time to keep backups for.
  • Added config report.
  • Added ASCII banner art when run in the console.
  • Added option to disable the ASCII banner art.
  • Refactored code.

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.

 

17 thoughts on “Log Manager Utility

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

  2. Hi Stuart,

    very nice script. Thank you for developing. I would like to place a feature request: Implementing custom naming of the ZIP-file over an additional Command Line Switch.

    That would be great!

    Best regards,
    Daniel

    Liked by 1 person

      1. Hello Mike,

        after updating the script from PSGallery to the newest version, there is an error message with following content when executing the script:

        Log-Manager.ps1 : The parameter ‘LogPath’ cannot be specified because it conflicts with the parameter alias of the same name for parameter ‘Source’.
        At line:1 char:1
        + Log-Manager.ps1 -LogPath C:\Import\ETLStaging\talend* -LogKeep 1 -Bac …
        + ~~~~~~~~~~~~~~~
        + CategoryInfo : MetadataError: (:) [], MetadataException
        + FullyQualifiedErrorId : ParameterNameConflictsWithAlias

        Just running “Log-Manager.ps1” from a PowerShell will cause the same error message.

        Would you please check it?

        Thank you and best regards,
        Daniel

        Liked by 1 person

        1. Hi Daniel,
          Apologies for the delay in responding – I’ve been offline unexpectedly. It appears to be a mistake in the script that I overlooked. I will update, test and update.

          Thanks for bringing this to my attention!

          -Mike

          Like

          1. Hello Mike,

            I have tested now. The variable “-LogsPath” does work now – bit only for full paths with no asterisks. You cannot use asterisks anymore in that variable like in the version second versions before. The new version does not recognize the path with asterisk as a valid path.
            When you try your example in the script you will see. If you replace the asterisks in your example with existing folders and files, it will work.

            Best regards,
            Daniel

            Liked by 1 person

  3. Hi Mike

    That’s just what I am seraching for … but I have just tried the script on Powershell 7.0
    At first try I have some messages like these:

    Add-Content: C:\Users\zanini\Documents\PowerShell\Scripts\Log-Manager.ps1:193
    Line |
    193 | Add-Content -Path $Log -Encoding ASCII -Value “$(Get-Date -Format .
    | ~~~~~~~~~
    | A parameter cannot be found that matches parameter name ‘Encoding’.
    Add-Content: C:\Users\zanini\Documents\PowerShell\Scripts\Log-Manager.ps1:213
    Line |
    213 | Add-Content -Path $Log -Encoding ASCII -Value “$(Get-Date .
    | ~~~~~~~~~
    | A parameter cannot be found that matches parameter name ‘Encoding’.
    2020-05-06 11:14:37 [INFO] There are no objects to process.

    It seems very strange that -Encoding was not found; so after a quick look with Get-Help I discovered that the parameter exists; I have tried to move it after the -Value in the instruction for logging like this

    Add-Content -Path $Log -Value “$(Get-DateFormat) [ERROR] $Event” -Encoding ASCII

    Solved! It seems that PS v.7 use to check some parameter parsing them ‘in order’ …

    Liked by 1 person

Leave a Reply to Daniel Cancel 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: