Solved

Powershell script to notify only new files

Posted on 2011-03-21
11
1,035 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

732 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