Solved

Powershell script to notify only new files

Posted on 2011-03-21
11
962 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

14 Experts available now in Live!

Get 1:1 Help Now