使用变量GetObject参数

时间:2014-12-14 16:24:11

标签: vbscript

以下代码提供了所需的结果:

Set myobj = GetObject("winmgmts:\\.\root\cimv2:Win32_LogicalDisk='c:'")

但是如何用字符串变量替换'c'?以下看似合乎逻辑,不起作用!

Set myobj = GetObject("winmgmts:\\.\root\cimv2:Win32_LogicalDisk='" & myvariable & ":'")

也不是

Set myobj = GetObject("""winmgmts:\\.\root\cimv2:Win32_LogicalDisk='" & myvariable & ":'""")

1 个答案:

答案 0 :(得分:1)

你的

Set myobj = GetObject("winmgmts:\.\root\cimv2:Win32_LogicalDisk='c:'")

'工作',但会抛出error。正确的是:

Set myobj = GetObject("winmgmts:\\.\root\cimv2:Win32_LogicalDisk='c:'")

然后 - 假设myvariable被(命名并且)初始化得很好 - 一切都很好:

>> s1 = "winmgmts:\\.\root\cimv2:Win32_LogicalDisk='c:'"
>> WScript.Echo "s1:", s1
>> myvariable = "c"
>> s2 = "winmgmts:\\.\root\cimv2:Win32_LogicalDisk='" & myvariable & ":'"
>> WScript.Echo "s2:", s2
>> WScript.Echo "s1 = s2:", CStr(s1 = s2)
>>
s1: winmgmts:\\.\root\cimv2:Win32_LogicalDisk='c:'
s2: winmgmts:\\.\root\cimv2:Win32_LogicalDisk='c:'
s1 = s2: True