Using WMI SMTP Event Consumer DSC resource

If you have not seen the earlier articles in this series, I had written about:

In today’s article, I will show you how the WMI SMTP event consumer DSC resource can be used. I will show you how this specific DSC resource is used and then show you a complete configuration script using this resource to send an email every time a removable device is inserted into the system.

Creating WMI SMTP Event Consumer

For obvious reasons, you need to specify the ToLine, FromLine, SMTPServer properties of the DSC resource. The Name property, as you might have guessed by now, uniquely identifies the resource instance. There are several optional properties. The Message property can be used to attach the body of the email and Subject property specifies the email subject. The CCLine and BCCLine properties are self-explained.

Here is the configuration script that is used to create an SMTP consumer instance.

cWMISMTPConsumer UFDSMTP {
   Name = 'UFDSMTP'
   Message = 'UFD drive with volume name Backup is attached.'
   Subject = 'UFD Detection'
   SMTPServer = 'smtp1.mymailserver.com'
   ToLine = 'ToUser@SomeDomain.com'
   FromLine = 'FromUser@AnotherDomain.com'
   Ensure = 'Present'
}

Make a note that there is no method to authenticate to the SMTP server. So, if the server you specified in the configuration requires authentication, the consumer action will fail. You can use the method I had explained in an earlier post to detect any failures in consumer actions.

The following complete configuration script helps us detect a volume change event and then respond to that using the SMTP event consumer by sending out an email to a specified email address.

Configuration BackuptoUFD {
    Import-DscResource -Module cWMIPermanentEvents 
    cWMIEventFilter UFDDetection {
       Name = 'UFDFilter'
       Query = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE
       TargetInstance ISA 'Win32_Volume' AND
       TargetInstance.Label='Backup'"
       EventNamespace = 'root\cimv2'
       Ensure = 'Present'
    }

    cWMISMTPConsumer UFDSMTP {
       Name = 'UFDSMTP'
       Message = 'UFD drive with volume name Backup is attached.'
       Subject = 'UFD Detection'
       SMTPServer = 'smtp1.mymailserver.com'
       ToLine = 'ToUser@SomeDomain.com'
       FromLine = 'FromUser@AnotherDomain.com'
       Ensure = 'Present'
    }

    cWMIEventBinding UFDCommandLineBinding {
       Filter = 'UFDFilter'
       Consumer = 'UFDSMTP'
       ConsumerType = 'SMTP'
       DependsOn = '[WMIEventFilter]UFDDetection','[WMISMTPConsumer]UFDSMTP'
       Ensure = 'Present'
    }
}
BackuptoUFD

This is the final post in this series of DSC resources for managing WMI permanent event filters, consumers, and bindings. I have a TODO list which I will share as a readme in the Github repo. Feel free to contribute code or report any bugs.

Share on: