Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell script to notify only new files

Posted on 2011-03-21
11
Medium Priority
?
1,094 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 6

Accepted Solution

by:
rwskas earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Screencast - Getting to Know the Pipeline

885 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