In my previous post I described how to create a WMI Event to watch a folder for changes. In this post I will make the WMI Event permanent so that it will work also after a computer restart.
Our script will have three parts:
- A definition of an event filter. I will reuse the same filter I used in Part I of this post.
- An event consumer which handles our event, in this case I will use a CommandLine Event Consumer.
- A part that binds the filter and the consumer together.
Let’s look at our WMI Event Query again:
$Query = @" Select * from __InstanceCreationEvent within 10 where targetInstance isa 'Cim_DirectoryContainsFile' and targetInstance.GroupComponent = 'Win32_Directory.Name="C:\\\\Data"' "@
We have defined a query that monitors the C:\Data folder for changes, with an interval of 10 seconds. Lets put this into a Event Filter:
$WMIEventFilter = Set-WmiInstance -Class __EventFilter ` -NameSpace "root\subscription" ` -Arguments @{Name="WatchFolder2EmailFilter"; EventNameSpace="root\cimv2"; QueryLanguage="WQL"; Query=$Query }
Next it is time for the Event Consumer. This is where we define what to do when the event is triggered. I will use the CommandLineEventConsumer that runs a command line. Another option would be to use ActiveScriptEventConsumer to run a script, but it only supports VBScript and we need PowerShell for our script.
If you want to read more about the different Event Consumers there’s more information on MSDN.
I have to define the Executable, CommandLine and the parameters to use for powershell.exe.
$WMIEventConsumer = Set-WmiInstance -Class CommandLineEventConsumer ` -Namespace "root\subscription" ` -Arguments @{Name="WatchFolder2EmailConsumer"; ExecutablePath = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"; CommandLineTemplate =" C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe –ExecutionPolicy Bypass -File c:\\WatchFolder2Email.ps1" }
As you can see I run the script file WatchFolder2Email.ps1 which we created in Part I of this post. I also specify ExecutionPolicy on the command line to make sure that I can run the script file.
Finally we bind our Filter and Consumer together:
Set-WmiInstance -Class __FilterToConsumerBinding ` -Namespace "root\subscription" ` -Arguments @{Filter=$WMIEventFilter; Consumer=$WMIEventConsumer }
There we go! Now we have a persistent WMI Event that watches our folder for changes, and then triggers our script that sends an email with the files as attachments.
Now we know how to create a persistent WMI Event. To remove it just run the following lines:
Get-WmiObject __EventFilter -namespace root\subscription -filter "name='WatchFolder2EmailFilter'" | Remove-WmiObject Get-WmiObject CommandLineEventConsumer -Namespace root\subscription -filter "name='WatchFolder2EmailConsumer'" | Remove-WmiObject Get-WmiObject __FilterToConsumerBinding -Namespace root\subscription -Filter "Filter = ""__eventfilter.name='WatchFolder2EmailFilter'""" | Remove-WmiObject
/ Andreas
Pingback: WMI Eventing: Watch folder and send files as email attachments, Part I | Tailspintoys - 365lab.net
How would you pass information about the event trigger in a permanent WMI register?