?
Solved

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

Posted on 2016-08-22
9
Medium Priority
?
448 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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