Powershell: Log Manager

Scripting

Stumbled across this post and thought interesting as we used to have similar problems with logs. We started using the app VisualCron to manage our logs but for whatever reason this could be a healthy costless alternative.

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

 

Source link

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Scripting
Install Vmware PowerCLI

PowerCLI is really easy to install now. From an Administrative Powershell window just enter: Install-Module -Name VMware.PowerCLI Minimum Powershell Version must be v3.   Offline Install of PowerCLI Accessing the PowerCLI Modules We’re now ready to download the PowerCLI modules. This task will require a system with internet access. This …

Scripting
Pass credentials with Powershell – 3 Ways

Interactive Here’s your typical scenario.   You have a script that requires credentials internally.  So to provide those credentials you would do something like $MyCredentials=GET-CREDENTIAL –credential “CONTOSO\Username” and you of course see a box like this normally on the screen Then you would type in the password and life would go …

Deployment
MDT – Joining a Computer to a Domain Using PowerShell

Using PowerShell scripts within a task sequence provides more flexibility than using the CustomSettings.ini file to join a computer to a domain. The parameters of the CustomSettings.ini file are common to any deployment you perform. That’s why creating a custom PowerShell script to join your domain will be customize to …