insert VBS variable within a single-quoted WMI query

Posted by jiaoziren on Stack Overflow See other posts from Stack Overflow or by jiaoziren
Published on 2010-06-15T06:32:52Z Indexed on 2010/06/15 19:12 UTC
Read the original article Hit count: 239

Filed under:

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!

© Stack Overflow or respective owner

Related posts about vbscript