[Win32utils-devel] win32ole properties question

Daniel Berger djberg96 at gmail.com
Sat Jan 18 12:41:37 UTC 2014


Hi

<snip>


> Here is a whole working translation code:
>
> require 'win32ole'
>
> intInterval = "2"
> strDrive = "C:"
> strFolder = "\\\\scripts\\\\"
> strComputer = "."
>
> # Connect to WMI
> objWMIService = WIN32OLE.connect("winmgmts:" +
>     "{impersonationLevel=impersonate}!//" +
>     strComputer + "\\root\\cimv2")
>
>
> # The query string
>
> strQuery =  "Select * From __InstanceOperationEvent" +
>      " Within " + intInterval +
>      " Where Targetinstance Isa 'CIM_DataFile'" +
>      " And TargetInstance.Drive='" + strDrive + "'" +
>      " And TargetInstance.Path='" + strFolder + "'"
>
> # Execute the query
>
> colEvents = objWMIService.ExecNotificationQuery(strQuery)
>
> # The loop
>
> while true do
>     # Wait for the next event
>     # Get SWbemEventSource object
>     # Get SWbemObject for the target instance
>
>     objEvent = colEvents.NextEvent()
>     objTargetInst = objEvent.TargetInstance
>
>     # Check the class name for SWbemEventSource
>     # It cane be one of the following:
>     # - __InstanceCreationEvent
>     # - __INstanceDeletionEvent
>     # - __InstanceModificationEvent
>     case objEvent.Path_.Class
>
>         # If it is file creation or deletion event
>         # just echo the file name
>
>         when "__InstanceCreationEvent"
>             puts "Created: " + objTargetInst.Name
>
>         when "__InstanceDeletionEvent"
>             puts "Deleted: " + objTargetInst.Name
>
>         # If it is file modification event,
>         # compare property values of the target and previous
>         # instance and echo the properties that have changed
>
>         when "__InstanceModificationEvent"
>
>             objPrevInst = objEvent.PreviousInstance
>
>             objTargetInst.Properties_.each do |objProperty|
>                 if objProperty.Value != objPrevInst.send(objProperty.Name)
>                     puts "Changed:        " +
>                          objTargetInst.Name
>                     puts "Property:       " +
>                          objProperty.Name
>                     puts "Previous value: " +
>                          objPrevInst.send(objProperty.Name)
>                     puts "New value:      " +
>                          objProperty.Value
>                     puts
>                 end
>             end
>     end
>
> end


Thanks! I see what I was doing wrong now.

Regards,

Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/win32utils-devel/attachments/20140118/21099bf1/attachment.html>


More information about the win32utils-devel mailing list