Solved

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

Posted on 2016-08-22
9
135 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
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.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

809 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