Log Manager Utility v1.7

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.

-Mike

Twitter – @Digressive

 

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
-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

2017-10-16 1.7

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

2017-10-09 1.6

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

2017-09-20 1.5

  • Improved log output to be more legible.

2017-06-21 1.4

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

2017-06-13 1.3

  • First public release.

 

PowerShell Code


<#PSScriptInfo .VERSION 1.7 .GUID 109eb5a2-1dd4-4def-9b9e-1d7413c8697f .AUTHOR Mike Galvin twitter.com/digressive .COMPANYNAME .COPYRIGHT (C) Mike Galvin. All rights reserved. .TAGS Log Manager Cleanup MDT Microsoft Deployment Toolkit IIS Internet Information Services .LICENSEURI .PROJECTURI https://gal.vin/2017/06/13/powershell-log-manager/ .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #>

<# .SYNOPSIS The script can cleanup and optionally archive old logs files. .DESCRIPTION The script can cleanup and optionally archive old logs files. This script can: Delete files and folder trees older than X days. Move files and folder trees to another location, after X days. Archive files and folder tress as a ZIP file, after X days. Please note: to send a log file using ssl and an SMTP password you must generate an encrypted password file. The password file is unique to both the user and machine. The command is as follows: $creds = Get-Credential $creds.Password | ConvertFrom-SecureString | Set-Content c:\foo\ps-script-pwd.txt .PARAMETER Path The root path that contains the files and or folders that the script should operate on. .PARAMETER Days The number of days from the current date that files created during should be untouched. .PARAMETER Backup 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. .PARAMETER WorkDir 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. .PARAMETER L The path to output the log file to. The file name will be Log-Manager-Log.log. .PARAMETER SendTo The e-mail address the log should be sent to. .PARAMETER From The from address the log should be sent from. .PARAMETER Smtp The DNS name or IP address of the SMTP server. .PARAMETER User The user account to connect to the SMTP server. .PARAMETER Pwd The password for the user account. .PARAMETER UseSsl Connect to the SMTP server using SSL. .EXAMPLE Log-Manager.ps1 -Path C:\inetpub\logs\LogFiles\W3SVC*\* -Days 30 -Backup \\nas\archive -WorkDir E:\scripts -L E:\scripts\log -SendTo me@contoso.com -From Log-Manager@contoso.com -Smtp exch01.contoso.com -User me@contoso.com -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 E:\scripts folder as a working directory. The log file of the scritp will be output to E:\scripts\log and emailed using an SSL connection. #>

## Set up command line switches and what variables they map to
[CmdletBinding()]
Param(
    [parameter(Mandatory=$True)]
    [alias("Path")]
    $Source,
    [parameter(Mandatory=$True)]
    [alias("Days")]
    $Time,
    [alias("Backup")]
    $Dest,
    [alias("WorkDir")]
    $Zip,
    [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 $Source –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, start log
    If ($LogPath)
    {
        $LogFile = "Log-Manager-Log.log"
        $Log = "$LogPath\$LogFile"

        ## If the log file already exists, clear it
        $LogT = Test-Path -Path $Log

        If ($LogT)
        {
            Clear-Content -Path $Log
        }

        Add-Content -Path $Log -Value "****************************************"
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log started"
        Add-Content -Path $Log -Value " "
    }

    If ($LogPath)
    {
        Add-Content -Path $Log -Value "$(Get-Date -Format G) The following objects are older than: $Time days and will be processed:"
        Get-ChildItem $Source | Select Name,LastWriteTime | Out-File -Append $Log -Encoding ASCII
        Add-Content -Path $Log -Value " "
    }

    ## 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 $Source | Where-Object CreationTime –lt (Get-Date).AddDays(-$Time) | Copy-Item -Destination $Zip\temp -Recurse -Force
        [io.compression.zipfile]::CreateFromDirectory("$Zip\temp", "$Zip\Logs-{0:yyyy-MM-dd-HH-mm}.zip" -f (Get-Date))
        Remove-Item $Zip\temp -Recurse
        Move-Item $Zip\Logs-*.zip $Dest
        Get-ChildItem $Source –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-$Time) | Remove-Item -Recurse

        If ($LogPath)
        {
            Add-Content -Path $Log -Value "$(Get-Date -Format G) Zip file created and copied to: $Dest"
        }
    }

    ## If the backup directory was configured, copy the files to the backup dir
    If ($Dest)
    {
        Get-ChildItem $Source | Where-Object CreationTime –lt (Get-Date).AddDays(-$Time) | Copy-Item -Destination $Dest -Recurse -Force

        If ($LogPath)
        {
            Add-Content -Path $Log -Value "$(Get-Date -Format G) Contents of the backup location: $Dest"
            Get-ChildItem $Dest | Select Name,LastWriteTime | Out-File -Append $Log -Encoding ASCII
        }
    }

    ## If no backup options were configured, or after doing the previous operations, remove the old files
    Get-ChildItem $Source –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-$Time) | Remove-Item -Recurse

    ## If log was configured stop the log
    If ($LogPath)
    {
        Add-Content -Path $Log -Value " "
        Add-Content -Path $Log -Value "$(Get-Date -Format G) Log finished"
        Add-Content -Path $Log -Value "****************************************"

        ## If email was configured, set the variables for the email subject and body
        If ($SmtpServer)
        {
            $MailSubject = "Log Manager Log"
            $MailBody = Get-Content -Path $Log | Out-String

            ## If an email password was configured, create a variable with the username and password
            If ($SmtpPwd)
            {
                $SmtpPwdEncrypt = Get-Content $SmtpPwd | ConvertTo-SecureString
                $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList ($SmtpUser, $SmtpPwdEncrypt)

                ## If ssl was configured, send the email with ssl
                If ($UseSsl)
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
                }

                ## If ssl wasn't configured, send the email without ssl
                Else
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds
                }
            }

            ## If an email username and password were not configured, send the email without authentication
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer
            }
        }
    }
}

## End

 

4 Comments Add yours

  1. Jake Nelson says:

    Great work. I’m looking at using this to manage some log files on a bunch of servers. I didn’t see a #Requires in your script. What version of PowerShell does it require?

    Like

    1. Mike Galvin says:

      I’ve only used it with PowerShell v4+ but it may work with previous versions.

      -Mike

      Like

  2. Stuart Cracraft says:

    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. Mike Galvin says:

      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.