Solved

Powershell script to send email via Gmail if file is older than specified time

Posted on 2016-08-22
9
152 Views
Last Modified: 2016-08-26
Hello,

I have a directory on a windows 2012 server where text files are dumped in order to be uploaded to a SQL server for ETL purposes; which generate a report to for a production site.

I need a powershell script ( or any script) to alert me via gmail (google apps) if the files in my directory are older than "X" amount time; 15 hours to be exact.

many thanks in advance

t
0
Comment
Question by:tobe1424
  • 4
  • 3
  • 2
9 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 41766450
Why not download and install the free version of PRTG for proper monitoring instead?
0
 

Author Comment

by:tobe1424
ID: 41766840
Thanks for pointing out other solutions

With respect to prtg, I already have nagios and currently working on implementing GLPI.

Do you think I can achieve the same with nagios?

I'm any case, how would any of these net mon tools monitor the date/time stamp ?

I have a feeling this will require some tweaking
0
 
LVL 18

Accepted Solution

by:
x-men earned 250 total points
ID: 41767256
if ($(ls * | ? {$_.CreationTime -gt $($(get-date)-$(new-timespan -Hours 15))} | measure-object).count -gt 0) {

    $EmailFrom = "mymail@gmail.com"
    $EmailTo = "tomail@gmail.com"
    $Subject = "files older than"
    $Body = "there are files older than 15 hours"
    $SMTPServer = "smtp.gmail.com"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("mymail@gmail.com", "my password");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

}
1
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:tobe1424
ID: 41767608
Thanks x-men let me give the the script a shot
0
 

Author Comment

by:tobe1424
ID: 41767641
Exception calling "Send" with "4" argument(s): "The SMTP server requires a secure connection or the client was not auth
enticated. The server response was: 5.5.1 Authentication Required. Learn more at"
At C:\Users\myuser\Documents\email-alert.ps1:11 char:21
+     $SMTPClient.Send <<<< ($EmailFrom, $EmailTo, $Subject, $Body)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException




I replaced the variables inside the quotations with my own values (kept the quotes) yet I received the message above related to auth
0
 
LVL 18

Expert Comment

by:x-men
ID: 41767703
It seams like you were unable to authenticate with Google. There is a security option on Google accounts to allow connections from legacy applications (it's just a name that Google gives to what ever's not from Google)... Will send you the complete walkthrough in about 12 hours.  (Got to get my beauty sleep...)
1
 

Author Comment

by:tobe1424
ID: 41767756
cheers x-men
0
 
LVL 18

Assisted Solution

by:x-men
x-men earned 250 total points
ID: 41768298
www.google.com - sign in - "My Account" (on the right corner in the app menu) - Sign-in & security - Connected apps & sites - Allow less secure apps: ON
1
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 250 total points
ID: 41768338
Sorry for the delay in getting back to you.  Yes PRTG will do this, we use it extensively.  Out of the box PRTG has folder monitors, we use it to raise alerts as soon as any file in a folder is older than X minutes/hours/days etc.

PRTG Guide to Folder monitoring
2

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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