Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Each of our jobs has a folder created by a batch file triggered by our accounting software at the time the job is entered. In that folder is a subfolder called "Sales Pictures". I would like to be notified by e-mail when files are created in any of the "Sales Pictures" folders under any job. Thankfully I don't need to go back and change every one of the 50000 or so sales pictures folders, only newly created folders. Is this possible in windows 2003 R2 using the command line with either no 3rd party software or free or open source software?

Thanks

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

I haven't tested the syntax but off the top of my head these PowerShell lines should work :

$query = "Select * from __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Drive='C:' AND TargetInstance.Path='\\mypath\\'"

Register-WmiEvent -Query $query -Action {send-mailmessage -to "me@mycomany.com" -from "server@mycompany.com" -subject "new file" -body " New file created " + ($event.SourceEventArgs.NewEvent.TargetInstance | Select -Expand FileName, Extension, Name)}

Where mypath is the name of the path and you'll need to put your own email addresses in. You may also have to add the -smtpserver parameter if the $PSEmailServer preference variable has not been set

This creates a temporary wmi event and consumer - meaning it works as long as the console is open. If you wanted a permanent provider you can create one with mofcomp or use a module like powerevents to make it a little easier to create the event. Permanent events will survive reboots etc.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.