How do I get raw VBScript command line arguments?

2019-09-10 02:24发布

问题:

This question already has an answer here:

  • What is the %* or $* argument list equivalent for VBScript? 2 answers

How do I get the entire command line in a .vbs file? I'm looking to process it myself, with all special characters/quotes still intact.

For example, the command:

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

I am NOT interested in:

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

These methods mangle and strip all quotes.

I want to get the raw arguments, unprocessed in any way:

/month:April /price:500 "Joe Smith" is "our" guy

回答1:

You can use 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

output:

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