Check eventlog status in powershell

When we monitor servers, we need to get notified if there are new errors and/or warnings in the Windows Eventlog.. Normally, it is the Application event log we check for these errors and warnings. The required functionality is that the script should check for errors and warnings on the current machine and if the number of event exceeds a maximum, the script should send an email to administrator to notify that some further investigation is needed on the server.

Here is the second powershell script I wrote today:

CHECK_EVENT_LOG_STATISTICS.PS1:

param([String]$logname="Application", [Int]$maxEventCount=10)

$since = ((get-date).addDays(-1));

$errwarnlist =  Get-Eventlog -logname $logname -after $since 
			-EntryType Error, Warning
$linecount = $errwarnlist | Measure-Object –Line

$msg = $env:COMPUTERNAME + " " + $logname + " eventlog has " 
       + $linecount.Lines  
       + " errors/warnings since " + $since.ToString("dd.MM.yyy HH:mm") 
       + ". Limit = " + $maxEventCount;

if ($linecount.Lines -gt $maxEventCount)
{
    $xto = john.doe@company.com
    $xfrom = "no-reply@company.com"
    $xsub = $env:COMPUTERNAME + ": " + $logname 
            + " eventlog exceeds limit for errors/warnings";

    Write-host $msg

    Send-MailMessage -To $xto -From $xfrom  
                 -Subject $xsub  -Body $msg 
                 -SmtpServer smtp.company.com
}
else
{
    Write-host $msg
}
Advertisements

Check service status in powershell

This week have been dedicated to baselining and monitoring server state. Our department is mainly monitoring SQL Server, Sharepoint  and Dynamic CRM servers for our customers. Therefore, it is very important to check if services for are running at all time. If they are not running we have to restart them, otherwise out customers will have downtime for the particular service. The powershell script below checks if a service exist and running. If it is not running the script is trying to restart the service. The script will send an email if the servcie is not installed on the server or can not be restarted.

CHECK_SERVICE_STATE.PS1:
param([String]$svcname="")

if ($svcname.Trim().Length -eq 0)
{
    Write-Host "Invalid service name" ;
    return ;
}

$svc = get-service $svcname -ErrorAction SilentlyContinue;
if ($svc -eq $null)
{
    $xto = "jon.doe@company.com"
    $xfrom = "no-reply@company.com"
    $xsub = $env:COMPUTERNAME + ": " + $svcname + " service error";
    $msg = $env:COMPUTERNAME + ": " + $svcname + " service does not exist";
    Write-host $msg
    Send-MailMessage -To $xto -From $xfrom  -Subject $xsub  
                     -Body $msg -SmtpServer smtp.company.com
    return;
}

$sts = $svc | Select Status;
if ($sts.Status.ToString() -eq "Stopped")
{
    Write-Host "Service " $svcname  " is STOPPED ==> trying to restart";
    Restart-Service $svcname ;
    $svc = get-service $svcname -ErrorAction SilentlyContinue;
    if ($svc -ne $null)
    {
        $sts = $svc | Select Status ;
        if ($sts.Status.ToString() -eq "Stopped")
        {
            $xto = "jon.doe@company.com"
            $xfrom = "no-reply@company.com"
            $xsub = $env:COMPUTERNAME + ": " + $svcname + " service error";
            $msg = $env:COMPUTERNAME + ": " + $svcname 
                          + " service could not be restarted";
            Write-host $msg
            Send-MailMessage -To $xto -From $xfrom  -Subject $xsub  
                             -Body $msg -SmtpServer smtp.company.com
            return
        }

        Write-Host $svcname " status = " $sts.Status.ToString();
    }
}
else
{
    Write-Host "Service " $svcname " is still RUNNING";
}