Solved

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

Posted on 2016-08-22
9
96 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

762 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

23 Experts available now in Live!

Get 1:1 Help Now