How do I get raw VBScript command line arguments? [duplicate]

血红的双手。 提交于 2020-01-06 15:41:56

问题


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


来源:https://stackoverflow.com/questions/40056204/how-do-i-get-raw-vbscript-command-line-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!