Using WMI Active Script Event Consumer DSC resource

In my previous article, I talked about using WMI Commandline Event Consumer DSC resource. In that article, I showed you how we can respond to a flash drive insertion event and create a backup copy of a local folder on the removal device. We already saw how to create an event filter to detect a removable drive insertion event. We will reuse that here. So, let’s directly go the discussion on how we can use the cWMI Active Script Event Consumer DSC resource.

Before we go ahead, understand that the only scripting engine supported by the Active Script Event Consumer is VBScript. You cannot invoke PowerShell scripts directly. However, you can use a VBScript wrapper to run a PowerShell script.

Creating Active Script Consumer

The cWMIActiveScriptConsumer DSC resource has two different ways of specifying the script to execute.

The first method is to specify a ScriptFileName that takes the complete path to a .vbs script. The second method is to use the ScriptText property to provide the VBScript fragment that needs to be executed. The MaximumQueueSize and ScriptingEngine are optional properties.

cWMIActiveScriptConsumer UFDScript {
   Name = 'UFDScript'
   ScriptText = '
      Set objFSO=CreateObject("Scripting.FileSystemObject")
      curDate = Year(Date) & "-" & Month(Date) & "-" & Day(Date)
      objFolder = TargetEvent.TargetInstance.DriveLetter & "\" & curDate
      Set bkFolder = objFSO.CreateFolder(objFolder)
      objFSO.CopyFolder "c:\Scripts", objFolder
      objFile.Close
    '
    Ensure = 'Present'
}

In the above configuration script, the TargetEvent.TargetInstance gives us access to the Win32_Volume instance object. Using this, we can retrieve the DriveLetter property for the attached removable disk.

You  can copy the contents of ScriptText property to a .vbs file and use that as the argument for the ScriptFileName property. Understand that the ScriptText and ScriptFileName are mutually exclusive.

Complete Configuration Script

After the event filter and event consumer instance creation, we need to bind them together. We have already seen an example of using WMIEventBinding DSC resource in an earlier article. So, here is the complete configuration script.

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' AND
                    TargetInstance.DriveType=2"
       EventNamespace = 'root\cimv2'
       Ensure = 'Present'
   }
   cWMIActiveScriptConsumer UFDScript {
       Name = 'UFDScript'
       ScriptText = '
           Set objFSO=CreateObject("Scripting.FileSystemObject")
           curDate = Year(Date) & "-" & Month(Date) & "-" & Day(Date)
           objFolder = TargetEvent.TargetInstance.DriveLetter & "\" & curDate
           Set bkFolder = objFSO.CreateFolder(objFolder)
           objFSO.CopyFolder "c:\Scripts", objFolder
           objFile.Close
       '
       Ensure = 'Present'
   }
   cWMIEventBinding UFDCommandLineBinding {
       Filter = 'UFDFilter'
       Consumer = 'UFDScript'
       ConsumerType = 'Script'
       DependsOn = '[WMIEventFilter]UFDDetection','[WMIActiveScriptConsumer]UFDScript'
       Ensure = 'Present'
   }
}
BackuptoUFD

This is it for today. In the next article, we will look at how to use the LogFile Event Consumer DSC resource.

Share on: