-1

I'm trying to pass a string parameter from a batch file to a vbs script that includes strings and I'm having some issues. I've tried a few variants but can't get it right.

_execute.vbs:

Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream(1)
Set stderr = fso.GetStandardStream(2)

stdout.Write WScript.Arguments.Item(0) & "... "
Dim objShell, oExec
Set objShell = WScript.CreateObject ("WScript.Shell")
intReturn = objShell.Run(WScript.Arguments.Item(1), 0, True)
if intReturn = 0 Then
    stdout.WriteLine "Done"
Else
    stderr.WriteLine "Error (Return Code: " & intReturn & ") trying to execute [" & WScript.Arguments.Item(1) & "]"
End If

  • First try:

    install.bat:

    @echo off
    cscript /nologo _execute.vbs "Installing IIS" "C:\Windows\SysWOW64\inetsrv\appcmd set site ""Default Web Site"" -name:Stream"
    

    Output:

    Installing IIS... Error (Return Code: 87) trying to execute [C:\Windows\SysWOW64\inetsrv\appcmd set site Default Web Site -name:Stream]

  • Second try:

    install.bat:

    @echo off
    cscript /nologo _execute.vbs 'Installing IIS' 'C:\Windows\SysWOW64\inetsrv\appcmd set site "Default Web Site" -name:Stream'
    

    Output:

    'Installing... C:\DEV_execute.vbs(8, 1) (null): The system cannot find the file specified.

  • Third try:

    install.bat:

    @echo off
    cscript /nologo _execute.vbs "Installing IIS" "C:\Windows\SysWOW64\inetsrv\appcmd set site ^"Default Web Site^" -name:Stream"
    

    Output:

    Installing IIS... Error (Return Code: 1168) trying to execute [C:\Windows\SysWOW64\inetsrv\appcmd set site ^Default]

How can I pass two parameters that are strings that may include quotes?

Ansgar Wiechers
  • 184,186
  • 23
  • 230
  • 299
THE JOATMON
  • 16,761
  • 34
  • 110
  • 200
  • 2
    Have you try to use backslash? `cscript /nologo _execute.vbs "Installing IIS" "C:\Windows\SysWOW64\inetsrv\appcmd set site \"Default Web Site\" -name:Stream"` – Brank Victoria Jan 18 '19 at 16:31
  • The argument parser will consume the quotes in the command line so you can't (no without your own argument handling code, more [here](https://stackoverflow.com/q/31095570/2861476)). A simple solution could be to store the command in a variable in the batch file and retrieve it inside the script using the `.Environment` collection of the `WScript.Shell` object. – MC ND Jan 18 '19 at 18:31

1 Answers1

-1

I ended up just using a single quote in the batch file

cscript /nologo _execute.vbs "Installing IIS" "C:\Windows\SysWOW64\inetsrv\appcmd set site 'Default Web Site' -name:Stream"

and replacing them with double quotes in the VBS file.

Replace(WScript.Arguments.Item(1),"'",chr(34))

It's not ideal, but it works for my uses.

THE JOATMON
  • 16,761
  • 34
  • 110
  • 200