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命令行.
没有等同于%*或$*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)
小智 1
没有一个。但这是相当微不足道的。
\n\nFor each ag in wscript.arguments\n CMDLINE = CMDLINE & " " & ag\nNext\nwscript.echo mid(CMDLINE, 2)\nRun Code Online (Sandbox Code Playgroud)\n\n或者
\n\nFor 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)\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\nC:\\Users\\User>cscript //nologo "C:\\Users\\User\\Desktop\\New Text Document (3).vbs" cat dog "mouse and cat"\ncat dog mouse and cat\nRun Code Online (Sandbox Code Playgroud)\n\n这适用于 VBScript 和 VBA。
\n\n这两个基础知识都是由其他程序托管的。它是收集命令行信息(如果有)的主机。在 wscript 的情况下,它是主机通过对象提供给 vbs 的,但在 IE/IIS 中托管时则不然。并且VBA具有宿主实现的功能(由Corel Office、Microsoft's Office和VB6实现)。
\n\nFunction 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.\nRun Code Online (Sandbox Code Playgroud)\n\n在幕后(我已经删除了没有解析行为的段落)(并注意 ANSI/Unicode 差异)
\n\nCommandLineToArgvW 函数
\n\n解析 Unicode 命令行字符串并返回一个以 null 结尾的 Unicode 字符串数组,其中包含该命令行中找到的各个参数以及参数计数,类似于标准 C 运行时 argv 和 argc 值。
\n\n句法
\n\nLPWSTR *CommandLineToArgvW( LPCWSTR lpCmdLine,\n int *pNumArgs\n);\nRun Code Online (Sandbox Code Playgroud)\n\n参数
\n\n此函数接受包含包含在引号中或不包含在引号中的程序名称的命令行。
\n\nCommandLineToArgvW 对后跟引号字符 (") 的反斜杠字符有特殊解释,如下所示:
\n\n2n 个反斜杠后跟一个引号会生成 n 个反斜杠\n后跟一个引号。
\n\n(2n) + 1 个反斜杠后跟一个引号,再次生成 n\n 个反斜杠后跟一个引号。
\n\nn 后面没有引号的反斜杠只会产生 n\n反斜杠。
获取命令行
\n\n检索当前进程的命令行字符串。
\n\nLPTSTR WINAPI GetCommandLine(void);\nRun 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