Run Command line via VBS with parameters

18,797

Solution 1

Working Code ended up being:

Dim objShell, strPath1, strAttr1, strAttr2, strAttr3
Set objShell = CreateObject ("WScript.Shell")

strPath1 = """C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"""
strAttr1 = " -T ws start "
strAttr2 = """C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"""
strAttr3 = " nogui"

objShell.Run strPath1 & strAttr1 & strAttr2 & strAttr3

Solution 2

i would replace objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3

with

objShell.Run strPath1 & strAttr1 & chr(34) & strAttr2 & chr(34) & strAttr3

or include the chr(34) before and after the strAttr2 variable

strAttr2 = chr(34) & "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" & chr(34)

btw chr(34) = "

Share:
18,797
craig24x
Author by

craig24x

Updated on June 04, 2022

Comments

  • craig24x
    craig24x almost 2 years

    Having a hard time with this one. I can run the following command from a command prompt successfully, but can't get it working with a VB script.

    From CMD:

    1. Change directory to C:\Program Files (x86)\VMware\VMware Workstation\
    2. then run: vmrun.exe -T ws start "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" nogui

    What I've tried in VBS:

    Dim objShell, strPath1, strAttr, strPath2 
    Set objShell = CreateObject ("WScript.Shell")
    
    strPath1 = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
    strAttr1 = " -T ws start "
    strAttr2 = "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"
    strAttr3 = " nogui"
    
    'WScript.Echo strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3 
    
    objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3 
    

    The error I get is: The system cannot find the file specified.

  • Gonen09
    Gonen09 about 3 years
    or create a variable that already has the quotes to concatenate cleaner var = chr(34) & strAttr2 & chr(34)