Solved

Powershell script to notify only new files

Posted on 2011-03-21
11
1,046 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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