0

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

meiskam
  • 11
  • 3

1 Answers1

-2

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
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • The only problem I see is that you can have two instances of the same script running. – MC ND Oct 15 '16 at 08:28
  • @MCND - no, you just put a streamlined version of this code into a library/into each file you need the command line. – Ekkehard.Horner Oct 15 '16 at 08:30
  • Sorry, I was talking about the same script in same disk file, with several instances running at the same time but with different arguments. – MC ND Oct 15 '16 at 08:36