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

时间:2015-06-28 02:30:08

标签: vbscript command-line-arguments wsh

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

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

受挫的例子:

cscript //nologo script.vbs /arg1:a -s "a b" 1 c /arg2:"x y" "d e" -l '3 4'

应该返回:

/arg1:a -s "a b" 1 c /arg2:"x y" "d e" -l '3 4'

(包括引号)。

我查看了WScript.Arguments,但它没有返回逐字命令行。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

x

For each ag in wscript.arguments
    CMDLINE = CMDLINE & " " & ag
Next
wscript.echo mid(CMDLINE, 2)

For each ag in wscript.arguments
    If Instr(Ag, " ") = True then
        CMDLINE = CMDLINE & " " & Chr(34) & ag & Chr(34)
    Else
        CMDLINE = CMDLINE & " " & ag
    End if
Next
wscript.echo mid(CMDLINE, 2)

这适用于VBScript和VBA。

这两个基础都由其他程序托管。收集命令行信息的主机(如果有)。它是主机,通过wscript的情况下的对象使其可用于vbs,但在IE / IIS中托管时则不可用。 VBA具有主机实现的功能(由Corel Office,Microsoft办公室和VB6实现)。

C:\Users\User>cscript //nologo "C:\Users\User\Desktop\New Text Document (3).vbs" cat dog "mouse and cat"
cat dog mouse and cat

引擎盖下(我没有删除任何解析行为段落)(并注意ANSI / Unicode差异)

CommandLineToArgvW函数

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

语法

Function Declaration
Function Command() As Variant
Function Command$() As String
Runtime Semantics.
Returns the argument portion of the implementation dependent command used to initiate execution of the currently executing VBA program.
The 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.

参数

此函数接受包含程序名的命令行,该程序名用引号括起或不用引号括起来。

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

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

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

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

<强> GetCommandLine

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

LPWSTR *CommandLineToArgvW(          LPCWSTR lpCmdLine,
    int *pNumArgs
);

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

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

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

相关问题