1

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

Albion
  • 465
  • 2
  • 6
  • 16

1 Answers1

2

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.

Jim B
  • 24,081
  • 4
  • 36
  • 60