什么是VBScript等效的%*或$*参数列表?

mva*_*nle 7 vbscript wsh command-line-arguments

是否有%*(批处理文件)或$*(bash脚本)参数列表等效于VBScript?

我想检索确切的命令行调用.

举例:

cscript //nologo script.vbs /arg1:a -s "a b" 1 c /arg2:"x y" "d e" -l '3 4'
Run Code Online (Sandbox Code Playgroud)

应该返回:

/arg1:a -s "a b" 1 c /arg2:"x y" "d e" -l '3 4'
Run Code Online (Sandbox Code Playgroud)

(包括引号).

我看过WScript.Arguments但它没有返回verbatim命令行.

MC *_* ND 7

没有等同于%*$*VBScript中.该WScript.Arguments集合隐藏了输入命令行,允许将参数作为集合内的项目进行访问.

我知道检索所需信息的唯一方法是查询当前进程的WMI,并从进程信息中读取命令行.

这将为您提供用于启动当前脚本的完整命令行.

Option Explicit

' We need a child process to locate the current script. 
Const FLAG_PROCESS = "winver.exe"

' WMI constants 
Const wbemFlagForwardOnly = 32

' Generate a unique value to be used as a flag
Dim guid
    guid = Left(CreateObject("Scriptlet.TypeLib").GUID,38)

' Start a process using the indicated flag inside its command line
    WScript.CreateObject("WScript.Shell").Run """" & FLAG_PROCESS & """ " & guid, 0, False

' To retrieve process information a WMI reference is needed    
Dim wmi
    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

' Query the list of processes with the flag in its command line, retrieve the 
' process ID of its parent process ( our script! ) and terminate the process 
Dim colProcess, process, myProcessID
    Set colProcess = wmi.ExecQuery( _ 
        "SELECT ParentProcessID From Win32_Process " & _ 
        "WHERE Name='" & FLAG_PROCESS & "' " & _ 
        "AND CommandLine LIKE '%" & guid & "%'" _ 
        ,"WQL" , wbemFlagForwardOnly _ 
    )
    For Each process In colProcess
        myProcessID = process.ParentProcessID
        process.Terminate
    Next 

' Knowing the process id of our script we can query the process list 
' and retrieve its command line
Dim commandLine
    set colProcess = wmi.ExecQuery( _ 
        "SELECT CommandLine From Win32_Process " & _ 
        "WHERE ProcessID=" & myProcessID _ 
        ,"WQL" , wbemFlagForwardOnly _ 
    )
    For Each process In colProcess
        commandLine = process.CommandLine
    Next 

' Done
    WScript.Echo commandLine
Run Code Online (Sandbox Code Playgroud)

  • @Bond,是的,它不是最轻量级的方法,但连接参数不会看到`/ test:"data"`和`/ test:data`或`/ test:"da""ta之间的区别"`.或者`/ test:"da \"ta"`和`/ test:"da\ta"`.参数集合将删除任何双引号,OP表示应该包括引号. (3认同)

小智 1

没有一个。但这是相当微不足道的。

\n\n
For each ag in wscript.arguments\n    CMDLINE = CMDLINE & " " & ag\nNext\nwscript.echo mid(CMDLINE, 2)\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者

\n\n
For each ag in wscript.arguments\n    If Instr(Ag, " ") = True then\n        CMDLINE = CMDLINE & " " & Chr(34) & ag & Chr(34)\n    Else\n        CMDLINE = CMDLINE & " " & ag\n    End if\nNext\nwscript.echo mid(CMDLINE, 2)\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
C:\\Users\\User>cscript //nologo "C:\\Users\\User\\Desktop\\New Text Document (3).vbs" cat dog "mouse and cat"\ncat dog mouse and cat\n
Run Code Online (Sandbox Code Playgroud)\n\n

这适用于 VBScript 和 VBA。

\n\n

这两个基础知识都是由其他程序托管的。它是收集命令行信息(如果有)的主机。在 wscript 的情况下,它是主机通过对象提供给 vbs 的,但在 IE/IIS 中托管时则不然。并且VBA具有宿主实现的功能(由Corel Office、Microsoft's Office和VB6实现)。

\n\n
Function Declaration\nFunction Command() As Variant\nFunction Command$() As String\nRuntime Semantics.\n\xef\x82\xa7\xef\x80\xa0Returns the argument portion of the implementation dependent command used to initiate execution of the currently executing VBA program.\n\xef\x82\xa7\xef\x80\xa0The runtime semantics of Command$ are identical to those of Command with the exception that the declared type of the return value is String rather than Variant.\n
Run Code Online (Sandbox Code Playgroud)\n\n

在幕后(我已经删除了没有解析行为的段落)(并注意 ANSI/Unicode 差异)

\n\n

CommandLineToArgvW 函数

\n\n
\n\n

解析 Unicode 命令行字符串并返回一个以 null 结尾的 Unicode 字符串数组,其中包含该命令行中找到的各个参数以及参数计数,类似于标准 C 运行时 argv 和 argc 值。

\n\n

句法

\n\n
LPWSTR *CommandLineToArgvW(          LPCWSTR lpCmdLine,\n    int *pNumArgs\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

参数

\n\n

此函数接受包含包含在引号中或不包含在引号中的程序名称的命令行。

\n\n

CommandLineToArgvW 对后跟引号字符 (") 的反斜杠字符有特殊解释,如下所示:

\n\n
    \n
  • 2n 个反斜杠后跟一个引号会生成 n 个反斜杠\n后跟一个引号。

    \n\n

    (2n) + 1 个反斜杠后跟一个引号,再次生成 n\n 个反斜杠后跟一个引号。

    \n\n

    n 后面没有引号的反斜杠只会产生 n\n反斜杠。

  • \n
\n\n

获取命令行

\n\n

检索当前进程的命令行字符串。

\n\n
LPTSTR WINAPI GetCommandLine(void);\n
Run Code Online (Sandbox Code Playgroud)\n\n

用 C 编写的 ANSI 控制台进程可以使用 main 函数的 argc 和 argv 参数来访问命令行参数。ANSI GUI应用程序可以使用WinMain函数的lpCmdLine参数来访问命令行字符串,不包括程序名称。main 和 WinMain 无法返回 Unicode 字符串的原因是 argc、argv 和 lpCmdLine 使用 LPSTR 数据类型作为参数,而不是 LPTSTR 数据类型。GetCommandLine 函数可用于访问 Unicode 字符串,因为它使用 LPTSTR 数据类型。

\n\n

要将命令行转换为 argv 样式的字符串数组,请调用 CommandLineToArgvW 函数。

\n\n

注意 操作系统向进程提供的命令行中的可执行文件名称不一定与调用进程向 CreateProcess 函数提供的命令行中的名称相同。操作系统可能会在没有完全限定路径的情况下提供的可执行名称前面添加完全限定路径。

\n

  • 带有空格的参数需要加引号。 (2认同)
  • `Instr(Ag, " ") = True` 违反了“切勿与 True/False 进行比较”的规则 (http://blogs.msdn.com/b/ericlippert/archive/2004/07/15/184431.aspx)。 (2认同)
  • `/arg2:"x y" "d e"` 成为 `WScript.Arguments` 集合中的 `/arg2:xy` 和 `de`。然后,您的代码会将其更改为 `"/arg2:x y" "d e"`。不是同一个意思。 (2认同)