问题描述
我对下面的片段感到非常沮丧:
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
我尝试用 varSrc 替换 c:\\kk 但是 WSH 似乎不可能在 WMI 查询中识别它 - 更不用说它在单引号内!
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!
我尝试过 chr(34) 但它没有用.如果可能,请帮忙.非常感谢!
I have tried chr(34) and it didn't work. Please help if possible. Much appreciated!
更新:
非常感谢您的回答,并为歧义道歉.
Thank you so much for the answer and apologies for ambiguities.
varSrc
应该是C:\\\\kk
".并且 WMI 查询无法识别它.让我举例说明:
varSrc
is supposed to be "C:\\\\kk
". and the WMI query couldn't recognize it. Let me explain by example:
查询应该被解析为:
SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' AND TargetInstance.GroupComponent= 'Win32_Directory.Name= ""c:\\\\KK""'
我已经测试了上面的查询并且它有效!但是,我需要的是将 c:\\\\kk
替换为取自用户输入的参数,在本例中为 varSrc
.我遇到的问题是语法是正确的,但是 WMI Query 会将 varSrc
从字面上理解为 'Directory.Name
';或者另一种方式 - 语法错误或无法解析的查询.
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.
如果查询是:
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""varSrc""'")
该脚本将正常运行,但不会监视新创建的文件.因为它将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.
如果查询是:
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name="""""&varSrc&"""""'")
它会产生一个错误:
vmove.vbs(15, 2) SWbemServicesEx: Invalid query
推荐答案
最有可能的是,varSrc
包含带有单个反斜杠的文件夹路径,而您的 WMI 查询需要四个反斜杠作为路径分隔符.在将 varSrc
插入查询之前,尝试将 \
替换为 \\\\
,如下所示:
Most probably, varSrc
contains the folder path with single backslashes, while your WMI query requires four backslashes as the path separator. Try doing a replace of \
with \\\\
in varSrc
before inserting it into the query, like this:
varSrc = Replace(varSrc, "\", "\\")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""" & varSrc & """'")
这篇关于在单引号 WMI 查询中插入 VBS 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!