-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfilemonitor.vbs
71 lines (58 loc) · 2.34 KB
/
filemonitor.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
' VBScript source code
' WMIFileEvents.vbs
Sub DoFileProcessing
WScript.Echo "Copying " & objTargetInst.Name & " to " & strHomeFolder & "\Desktop\"
On Error Resume Next
fso.CopyFile objTargetInst.Name , strHomeFolder & "\Desktop\"
WScript.Echo "file copied to " & strHomeFolder & "\Desktop\"
On Error Resume Next
fso.DeleteFile objTargetInst.Name
strFilename = objTargetInst.Name
strFilename = strHomeFolder & "\Desktop\" & Replace(LCase(strFilename), LCase(strSharePath), "")
WScript.Echo "New file-path " & strFilename
if InStr(LCase(strFilename), "\start.bat") > 0 Then
On Error Resume Next
oShell.run strFilename
WScript.Echo "executed " & strFilename
end if
End Sub
Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem" )
For Each objItem in colItems
strComputerName = objItem.Name
WScript.Echo "Computer Name: " & strComputerName
Next
intInterval = "2"
strDrive = "z:"
strFolder = "\\" & strComputerName & "\\"
strComputer = "."
strSharePath = strDrive & "\" & strComputerName & "\"
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject( "winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2" )
strQuery = _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'" _
& " And TargetInstance.Path='" & strFolder & "'"
Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
WScript.Echo "Monitoring events...from " & strDrive & strFolder
oShell.LogEvent 4, "APTC file monitoring started"
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "Created: " & objTargetInst.Name
DoFileProcessing
Case "__InstanceDeletionEvent"
WScript.Echo "Deleted: " & objTargetInst.Name
Case "__InstanceModificationEvent"
DoFileProcessing
End Select
Loop