Solved

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

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

Author Comment

by:tobe1424
ID: 41767608
Thanks x-men let me give the the script a shot
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A safe way to clean winsxs folder from your windows server 2008 R2 editions
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now