如何获取原始VBScript命令行参数?

时间:2016-10-15 07:14:50

标签: vbscript cmd command-line-arguments

如何在.vbs文件中获取整个命令行?我希望自己处理它,所有特殊字符/引号仍然完整。

例如,命令:

cscript.exe example.vbs /month:April /price:500 "Joe Smith" is "our" guy 

我对以下内容不感兴趣:

WScript.Arguments.Named.Item("month")
= April

WScript.Arguments.Item(2)
= Joe Smith

Dim StrArgs
For Each arg In WScript.Arguments
  StrArgs = StrArgs & " " & arg
Next
= /month:April /price:500 Joe Smith is our guy

这些方法会破坏并删除所有引号。

我希望以任何方式获取未经处理的原始参数:

  

/月:4月/价格:500“乔史密斯”是“我们的”家伙

1 个答案:

答案 0 :(得分:-2)

您可以使用WMI:

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim oWMI : Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
Dim oCol : Set oCol = oWMI.ExecQuery( _
  "SELECT Commandline FROM Win32_Process", _
  "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Dim oElm
For Each oElm In oCol
   If Instr(oElm.CommandLine, "40056204.vbs") Then
      WScript.Echo "CommandLine: " & oElm.CommandLine
   End If
Next

输出:

cscript 40056204.vbs /month:April /price:500 "Joe Smith" is "our"      guy
CommandLine: cscript 40056204.vbs /month:April /price:500 "Joe Smith" is "our"      guy