2

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.

craig24x
  • 61
  • 1
  • 1
  • 4
  • I think you need *double* double quote every path with spaces – Yu Jiaao Mar 31 '17 at 01:13
  • Thanks! That was it. It just looked off when viewing the echo output, but it works :) – craig24x Mar 31 '17 at 02:14
  • The full code ended up as: `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" 'WScript.Echo strPath1 & strAttr1 & strAttr2 & strAttr3 'objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3 objShell.Run strPath1 & strAttr1 & strAttr2 & strAttr3` – craig24x Mar 31 '17 at 02:16
  • Possible duplicate of [Run Command Line & Command From VBS](http://stackoverflow.com/questions/16087470/run-command-line-command-from-vbs) – user692942 Mar 31 '17 at 10:28

2 Answers2

4

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
craig24x
  • 61
  • 1
  • 1
  • 4
0

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) = "

luisB
  • 9
  • 3
  • or create a variable that already has the quotes to concatenate cleaner var = chr(34) & strAttr2 & chr(34) – Gonen09 Apr 07 '21 at 01:01