Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert VBS variable within a single-quoted WMI query
    text
    copied!<p>I am really frustrated by the snippet below:</p> <pre><code>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:" _ &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2") Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _ &amp; "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _ &amp; "TargetInstance.GroupComponent= " _ &amp; "'Win32_Directory.Name=""c:\\\\kk ""'") Do Set objLatestEvent = colMonitoredEvents.NextEvent WScript.Echo objLatestEvent.TargetInstance.PartComponent Loop Else WScript.Echo "Bazinga" End If </code></pre> <p>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!</p> <p>I have tried chr(34) and it didn't work. Please help if possible. Much appreciated!</p> <p><strong>UPDATE:</strong></p> <p>Thank you so much for the answer and apologies for ambiguities.</p> <p><code>varSrc</code> is supposed to be "<code>C:\\\\kk</code>". and the WMI query couldn't recognize it. Let me explain by example:</p> <p>The query should be parsed in such:</p> <pre><code>SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' AND TargetInstance.GroupComponent= 'Win32_Directory.Name= ""c:\\\\KK""' </code></pre> <p>I have tested the query above and it works! However what I need is to replace <code>c:\\\\kk</code> with an argument taken from user input, which in this case, is <code>varSrc</code>. The problem I have encountered is either the syntax is correct however WMI Query would be taking <code>varSrc</code> literally as the '<code>Directory.Name</code>'; Or the other way - the syntax error or unparsable query.</p> <p>If the query is:</p> <pre><code>("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _ &amp; "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _ &amp; "TargetInstance.GroupComponent= " _ &amp; "'Win32_Directory.Name=""varSrc""'") </code></pre> <p>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.</p> <p>If the query is:</p> <pre><code>Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _ &amp; "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _ &amp; "TargetInstance.GroupComponent= " _ &amp; "'Win32_Directory.Name="""""&amp;varSrc&amp;"""""'") </code></pre> <p>It will generate an error:</p> <pre><code>vmove.vbs(15, 2) SWbemServicesEx: Invalid query </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload