【发布时间】:2010-11-16 08:46:30
【问题描述】:
正如我自己阅读和体验的那样,VBScript 会从参数中删除所有双引号。有谁知道解决这个问题的方法?如何将双引号传递到脚本中?
【问题讨论】:
-
你的意思是在 Windows Script Host 中吗?
标签: vbscript
正如我自己阅读和体验的那样,VBScript 会从参数中删除所有双引号。有谁知道解决这个问题的方法?如何将双引号传递到脚本中?
【问题讨论】:
标签: vbscript
如果该参数 需要 引号,您可以使用命名参数来识别它,然后用双引号将值括起来
dim arg
if WScript.Arguments.Named.Exists("a") then
arg = WScript.Arguments.Named("a")
arg = chr(34) & arg & chr(34)
end if
并因此使用:
cscript test.vbs /a:"a parameter"
但如果您只想保留引号(如果提供),这将无济于事。不过,单引号被接受,因此您也可以使用单引号(或其他字符/字符串)并使用Replace(arg, "'", chr(34)) 转换为双引号。
【讨论】:
此脚本将按原样获取命令行,将双引号和所有内容添加到名为 strLine 的变量中,并显示它:
Set objSWbemServices = GetObject("WinMgmts:Root\Cimv2")
Set colProcess = objSWbemServices.ExecQuery("Select * From Win32_Process")
For Each objProcess In colProcess
If InStr (objProcess.CommandLine, WScript.ScriptName) <> 0 Then
strLine = Mid(objProcess.CommandLine, InStr(objProcess.CommandLine , WScript.ScriptName) + Len(WScript.ScriptName) + 1)
End If
Next
WScript.Echo(strLine)
所以运行它:
cscript scriptname.vbs "option" ""other option"" third option
会导致:
"option" ""other option"" third option
【讨论】:
您可以像在https://stackoverflow.com/a/13212628/1752986 中一样获取当前 PID,而不是检查包含 WScript.ScriptName 的命令行
【讨论】:
编辑:误解了这个问题,所以这里有新的答案:
我认为你不能以任何方式做到这一点。但是,解决方法可能是使用 Win32_Process 类的 CommandLine 属性,我认为这应该可以为您提供完整的命令行。
例如试试这个脚本:
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set processes = wmi.ExecQuery("SELECT * FROM Win32_Process")
For Each proc in processes
If InStr(proc.CommandLine, "double quotes") > 0 Then
wscript.echo proc.CommandLine
End IF
Next
参数为:"some long commandline enclosed in double quotes here"
【讨论】:
cscript.exe test.vbs "parameter"
C:\> test.vbs "argument".
我不确定这是否有效,但在传递参数时,我猜你可以做类似的事情 -
chr(34) + <your argument string> + chr(34)
chr(34) 代表双引号。
【讨论】:
不幸的是,我不知道任何传递双引号的转义方法,因为它是一个参数分隔符。我所能建议的就是修改你的脚本,或者从那里添加引号。
【讨论】:
这里的答案借鉴/结合了其他一些,并根据脚本的 pid 查询进程信息:
Function ArgumentsString()
Dim nPid : nPid = ThisProcessId()
For Each oPrc In GetObject("winmgmts:\\.\root\cimv2").ExecQuery(_
"Select * From Win32_Process Where ProcessId=" & nPid )
Exit For : Next
ArgumentsString = Mid( oPrc.CommandLine, _
InStr(oPrc.CommandLine, WScript.ScriptName) + _
Len(WScript.ScriptName) + 1 )
End Function
Function ThisProcessId()
ThisProcessId = 0
Dim sTFile, oPrc
With CreateObject("Scripting.FileSystemObject")
sTFile = .BuildPath(.GetSpecialFolder(2), "sleep.vbs")
With .OpenTextFile(sTFile, 2, True)
.Write "WScript.Sleep 1000"
End With
End With
With CreateObject("WScript.Shell").Exec("WScript " & sTFile)
For Each oPrc In GetObject("winmgmts:\\.\root\cimv2").ExecQuery(_
"Select * From Win32_Process Where ProcessId=" & .ProcessID)
Exit For : Next
ThisProcessId = oPrc.ParentProcessId
End With
End Function
【讨论】: