Solved

PowerShell Script for alerting on when logs are modified or exceed a certain size

Posted on 2016-08-30
5
26 Views
Last Modified: 2016-09-01
Hi All

I am working on a PowerShell script that will alert on:

1) If a log file has been written to in the last 24 hours
2) If a log file exceeds x MB in size

Currently when I run the script I get 14 emails when running the script instead of the expected 1 as no files have been written to in several days and only 1 file is over 10MB in size.

# Name   :   Log file Alerts (Modified in last 24 hours or exceeded 10MB)
# Date   :   
# Author :   
#
# This script will check the date modified of log files and alert if they have been written to in the last 24 hours
# It will alert to email when the log files exceeds X MB in size

##############  User editable variables ##############
$Server = "8300-15041"
$LogType = "CDs & Notification Server Log File Checks"
$log = 'C:\MATSTEST\'
$LogURL = "\\8300-15041\c$\MATSTEST"
$MaxLogSize = 10MB

##############  Set Email variables ##########
$smtp = "mail.xxxxxxx.xxx" 
#$Recipients = "Application Support <matthew.emery@xxxxxxx.xxx>" 
[String[]]$Recipients = "matthew.emery@xxxxxxx.xxx" #Use for multiple recipients
$from = "noreply@xxxxxxx.xxx"
$subject = "Urgent! $Server $LogType Errors exist within the last 24 hours"
$subject2 = "Urgent! $Server $LogType Log file exceeded 10MB"
$body  = "Please log into <b><font color=red>$Server</font></b> and check $LogURL Log files<br>"
$body += "Click Here"

############## Search log directory for .log files written to within the last 24 hours ############## 

get-item $Log\*.log -Exclude "Notification Server (NOTIFICATION)_pager*" | ForEach-Object {


    ############## Send email alert if log file has been written to within the last 24 hours ##############
    IF ($_.LastWriteTime -gt (get-date).AddDays(-1)) {
        Send-MailMessage    -From $from -To $Recipients -Subject "$subject" -Body "$body" -BodyAsHtml -Priority high -Smtpserver $SMTP;       
    }

    ############## Email alert if log file has grown to over 10MB ##############
    $LogSize = Get-ChildItem $log | select length -ExpandProperty Length
    IF ($LogSize -gt $MaxLogSize) {
    Send-MailMessage    -From $from -To $Recipients -Subject "$subject2" -Body "$body" -BodyAsHtml -Priority high -Smtpserver $SMTP;
    }
}

else {exit}

Open in new window


I nearly forgot ...... As per line 27 I want to add further exceptions such as 'Notification Server (NOTIFICATION)_email' but I am not sure what formatting to use when using multiple exceptions

Mail Server and email addresses have been removed prior to posting the code

Any help on this issue would be really appreciated.
0
Comment
Question by:IM&T SRFT
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 41776671
The problem is with line 36.  Every time through the loop started on line 27, you were doing another loop searching for the same files.  If you just change line 36 to the following I think you're good.
    $LogSize = $_.Length

Open in new window

0
 

Author Comment

by:IM&T SRFT
ID: 41779329
Hi Footech,

Thank you so much that worked a treat!

I don't suppose you could advise about the other part where I wanted to add further exceptions to line 27.  I want to add 'Notification Server (NOTIFICATION)_email' to the exception but not sure what character separators to use for additional exceptions.
0
 
LVL 39

Expert Comment

by:footech
ID: 41779571
Sorry, didn't see that earlier.
The simplest way to find the answer is to read the help for the cmdlet.  If I remember correctly, you can just specify a string array (i.e. multiple items separated by commas).  For example:
-exclude "one*","two*"
0
 

Author Closing Comment

by:IM&T SRFT
ID: 41779803
Thank you for your help it is very much appreciated!

Have not quite nailed the exclusions but nearly there.
0
 
LVL 39

Expert Comment

by:footech
ID: 41780151
Also, more complex filtering could be done by piping to Where-Object and setting conditions there.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question