This is a test script that I am trying to get to work before I
incorporate it into the real thing. I am trying to get the
__InstanceModificationEvent to cooperate. Creation & Deletion events
work great. I can't figure out what triggers the Modification event.
I just want it to tell me if a file has been modified. By that I mean
if there is a file in the C:\X directory and a newer version of the
file is copied on top of the existing one, I would think that the
Modification Event would be triggered. Am I wrong. Someone please
help.


NoSeconds = "2"
MonitoredFolder = "C:\X"
CorrectMonitoredFolder = Replace( MonitoredFolder, "\", "\\\\" )
strComputer = "."
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objWMIService = GetObject( "winmgmts:" &_
"{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2" )
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN " & NoSeconds & "
WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""" & CorrectMonitoredFolder & """'" )
Do While True
Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "Created"
Case "__InstanceModificationEvent"
WScript.Echo "Modified"
Case "__InstanceDeletionEvent"
WScript.Echo "Deleted"
End Select
Loop

RE: Event Problems by urkec

urkec
Tue Jun 19 12:03:09 CDT 2007


"inthepickle" wrote:

> This is a test script that I am trying to get to work before I
> incorporate it into the real thing. I am trying to get the
> __InstanceModificationEvent to cooperate. Creation & Deletion events
> work great. I can't figure out what triggers the Modification event.
> I just want it to tell me if a file has been modified. By that I mean
> if there is a file in the C:\X directory and a newer version of the
> file is copied on top of the existing one, I would think that the
> Modification Event would be triggered. Am I wrong. Someone please
> help.
>
>
> NoSeconds = "2"
> MonitoredFolder = "C:\X"
> CorrectMonitoredFolder = Replace( MonitoredFolder, "\", "\\\\" )
> strComputer = "."
> Set objFSO = CreateObject( "Scripting.FileSystemObject" )
> Set objWMIService = GetObject( "winmgmts:" &_
> "{impersonationLevel=impersonate}!\\" &_
> strComputer & "\root\cimv2" )
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> ("SELECT * FROM __InstanceOperationEvent WITHIN " & NoSeconds & "
> WHERE " _
> & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
> & "TargetInstance.GroupComponent= " _
> & "'Win32_Directory.Name=""" & CorrectMonitoredFolder & """'" )
> Do While True
> Set objEventObject = colMonitoredEvents.NextEvent()
> Select Case objEventObject.Path_.Class
> Case "__InstanceCreationEvent"
> WScript.Echo "Created"
> Case "__InstanceModificationEvent"
> WScript.Echo "Modified"
> Case "__InstanceDeletionEvent"
> WScript.Echo "Deleted"
> End Select
> Loop
>
>

I altered your script to monitor all instances of CIM_DataFile in 'C:\x\'.
It looks like it correctly reports modification events, but I only tested it
a couple of times.

NoSeconds = "2"
MonitoredDrive = "C:"
MonitoredFolder = "\x\"
strComputer = "."

MonitoredFolder = Replace _
( MonitoredFolder, "\", "\\" )

Set objWMIService = GetObject( "winmgmts:" &_
"{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2" )

strQuery = _
"SELECT * FROM __InstanceOperationEvent" _
& " WITHIN " & NoSeconds _
& " WHERE Targetinstance ISA 'CIM_DataFile'" _
& " AND TargetInstance.Drive='" & MonitoredDrive & "'"_
& " AND TargetInstance.Path='" & MonitoredFolder & "'"

Set colMonitoredEvents = objWMIService. _
ExecNotificationQuery (strQuery)

Do While True

Set objEventObject = _
colMonitoredEvents.NextEvent()

Select Case objEventObject.Path_.Class

Case "__InstanceCreationEvent"
WScript.Echo "Created: " _
& objEventObject.TargetInstance.Name

Case "__InstanceDeletionEvent"
WScript.Echo "Deleted: " _
& objEventObject.TargetInstance.Name

Case "__InstanceModificationEvent"

If objEventObject.TargetInstance.LastModified <> _
objEventObject.PreviousInstance.LastModified then
WScript.Echo "Modified: " _
& objEventObject.TargetInstance.Name
End If

If objEventObject.TargetInstance.LastAccessed <> _
objEventObject.PreviousInstance.LastAccessed then
WScript.Echo "Accessed: " _
& objEventObject.TargetInstance.Name
End If

End Select

Loop


Hope this is of some help.

--
urkec

Re: Event Problems by inthepickle

inthepickle
Tue Jun 19 12:27:29 CDT 2007

thanks urkec - it works like a charm