Hi all, I am really frustrated by the snippet below:
Dim objFSO, varSrc, varDest, varExt
Set objFSO = CreateObject("Scripting.FileSystemObject")
varSrc = WScript.Arguments(0)
varDest = WScript.Arguments(1)
varExt = WScript.Arguments(2)
If objFSO.FolderExists(varSrc) Then
WScript.Echo varSrc
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\kk ""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
WScript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
Else
WScript.Echo "Bazinga"
End If
I tried to replace c:\\kk with varSrc however it seems impossible for WSH to recognize it in WMI query - not to mention it was within a single quote!
I have tried chr(34) and it didn't work. Please help if possible. Much appreciated!
UPDATE:
Thank you so much for the answer and apologies for ambiguities.
varSrc
is supposed to be "C:\\\\kk
". and the WMI query couldn't recognize it. Let me explain by example:
The query should be parsed in such:
SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' AND TargetInstance.GroupComponent= 'Win32_Directory.Name= ""c:\\\\KK""'
I have tested the query above and it works! However what I need is to replace c:\\\\kk
with an argument taken from user input, which in this case, is varSrc
. The problem I have encountered is either the syntax is correct however WMI Query would be taking varSrc
literally as the 'Directory.Name
'; Or the other way - the syntax error or unparsable query.
If the query is:
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""varSrc""'")
The script will run without any error however it's not doing its job monitoring the newly created files. Because it parse the 'Directory.Name' as literal 'varSrc' instead of user-input argument.
If the query is:
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name="""""&varSrc&"""""'")
It will generate an error:
vmove.vbs(15, 2) SWbemServicesEx: Invalid query