Solved

Powershell script to notify only new files

Posted on 2011-03-21
11
993 Views
Last Modified: 2012-05-11
Hi Experts,

I have a production server executing files continuously . Sometimes i get files which are not in proper format hence they are not processed and they stay in there forever. I would like to have a script that will run every 5 mins on a specific folder to to check for newer files . I would like to have only new files as these files contains reason for failures. I would like file name to be sent via email.


This question is a followup of http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26895555.html
0
Comment
Question by:santoshmotwani
  • 6
  • 3
  • 2
11 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35187687
I am a little confused, what was missing in the earlier solution/what is the additional functionality required?

regards
Prashanth

0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35187725
in this script i only want to email new files ... does not matter if they stay there for months.

this folder actually contain logs ... as soon as something stops working a new text file is generated in this folder . I want whenever that file is generated it should be sent via email.

Thnx for help
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35187857
How often will you be scheduling the script to run?

regards
Prashanth
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 6

Accepted Solution

by:
rwskas earned 500 total points
ID: 35192706
This script is assuming each file will have a unique name? If not, let me know and I will modify. Basically, it will get the names of files in your dir, check that against a logfile of already sent files, if its not in the log it will email you the file and add the name to the log, if it is in the log, it wont do anything (as you would have already been notified)

You will need to change the file and email params.
#Email Function
Function Email {
Param($Attach)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $Body)
Foreach ($msgAttach in $Attach) {
$Attachment = new-object Net.Mail.Attachment($msgAttach.Fullname)
$MailMessage.Attachments.Add($Attachment)
}
$smtp.Send($MailMessage)
}

#File Params
$LogFile = "C:\MyLogFile.txt"
$MonitorDirectory = "C:\FolderI want to Monitor"
#Email Params
$EmailFrom = "SomeAddress@Yourdomain.com"
$EmailTo = "SomeAddress@Yourdomain.com"
$subject = "New Files detected"
$body = "Attached are new files which have been detected "
$smtpServer = "ict0mbx05.kochind.com"

#Script begins
$SentFiles = Get-Content $LogFile
$NewFiles = Get-ChildItem $MonitorDirectory | Where {$SentFiles -NotContains $_.FullName}
Foreach ($NewFile in $NewFiles) {
	$FileName = $NewFile.FullName
	Email $NewFile
	Add-Content $LogFile $FileName
}

Open in new window

0
 
LVL 16

Author Closing Comment

by:santoshmotwani
ID: 35195171
thanks for your help.

Thats exactly what i wanted
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35195641
@rwskas

If i add another email address , it does not send attachments.....

Can you please help
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35196141
Can i please make a change, that it should only send email if there is a new file.
0
 
LVL 6

Expert Comment

by:rwskas
ID: 35198143
Can you post what you have changed? It should work that only a new file is emailed.
To add additional email addresses you should be doing:
$emailto = "someemail@domain.com, anotheremail@domain.com"
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35202992
@rwskas :

The script is working fine , it works fine for additional email as well.

But what i want is not to send blank emails, at this moment it is sending blank emails because of no new files. I want it to send a email only when a new file is created.

Thanks for your help
0
 
LVL 6

Expert Comment

by:rwskas
ID: 35203785
Modified piece attached should take care of that - sorry I overlooked it.
If ($NewFiles) {
Foreach ($NewFile in $NewFiles) {
        $FileName = $NewFile.FullName
        Email $NewFile
        Add-Content $LogFile $FileName
}
}

Open in new window

0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35203837
Thnx mate !!!!
0

Featured Post

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

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.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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