17

As I read and experienced for myself VBScript removes all double quotes from and argument. Does anyone know a way around this? How to pass double quotes into the script?

Dani
  • 461
  • 2
  • 5
  • 7

7 Answers7

11

If that parameter requires quotes you could use a named parameter to identify it and then enclose the value with the double quotes

dim arg
if WScript.Arguments.Named.Exists("a") then
    arg = WScript.Arguments.Named("a")
    arg = chr(34) & arg & chr(34)
end if

and used thus:

cscript test.vbs /a:"a parameter"

but this doesn't help if you merely want to keep quotes if supplied. Single quotes are accepted though, so you could alternatively use single quotes (or another character/string) and do a Replace(arg, "'", chr(34)) to convert to double-quotes.

8

This script will get the command line as it is, with double quotes and everything to a variable called strLine, and display it:

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)

So running that with:

cscript scriptname.vbs "option" ""other option"" third option

would result in:

"option" ""other option"" third option
bensiu
  • 24,660
  • 56
  • 77
  • 117
Kolatatti
  • 81
  • 1
  • 2
2

Rather than check for a command line to include WScript.ScriptName, you can get the current PID like in https://stackoverflow.com/a/13212628/1752986

Community
  • 1
  • 1
Toughy
  • 767
  • 6
  • 5
1

Edit: Misunderstood the question so new answer here:

I don't think you can do that in any way. However, a work around might be to use the CommandLine property of the Win32_Process class, which should get you the complete commandline I think.

For example try this script:

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

With the parameters as: "some long commandline enclosed in double quotes here"

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • I think the @Dani means as a parameter to a script, ie `cscript.exe test.vbs "parameter"` –  Nov 16 '10 at 09:00
  • 1
    He mean't: `C:\> test.vbs "argument"`. – Ruel Nov 16 '10 at 09:00
  • That will work. Now all that is left is the get the PID for the running VBS process :) – Dani Nov 16 '10 at 12:12
  • 1
    If you're interested, this how it works: 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 WScript.Echo objProcess.Name, _ objProcess.ProcessId, _ objProcess.CommandLine End If Next – Dani Nov 16 '10 at 12:25
1

I am not sure if this works, but while passing parameter I guess you can do something like -

chr(34) + <your argument string> + chr(34)

The chr(34) stands for double quotes.

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • 1
    This, unfortunately, just causes the string literal chr(34) to appear as a parameter (or part of a parameter). For example, if the only parameter is chr(34)+foo+chr(34) then the parsed parameter will be exactly that. – jveazey Nov 17 '10 at 03:16
  • @John L Veazey - That is exactly what OP wants from his question right? I did not understand your point. Sorry. – Sachin Shanbhag Nov 17 '10 at 09:45
0

Unfortunately, I do not know any escape methods to pass double-quotes because its an argument delimiter. All I can suggest is to modify your script, or add the quotes from there.

Ruel
  • 15,438
  • 7
  • 38
  • 49
0

Here's answer that draws upon/combines some of the others here and queries the process info based on the script's 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
BuvinJ
  • 10,221
  • 5
  • 83
  • 96