How to run a command in vmware using vmrun, command is (echo %PROGRAMFILES%)

user2511126 picture user2511126 · Jan 28, 2014 · Viewed 7.6k times · Source

How to run a command in vmware using vmrun, command is (echo %PROGRAMFILES%) on the guest machine.. and the guest machine should return a value of the command result... how to do this??? please suggest

Answer

samwyse picture samwyse · Jul 10, 2014

I needed to do something similar and found this unanswered question. Here's my solution.

@ECHO OFF

REM Set up abbreviations that we'll be using a lot.
SET VMRUN="C:\Program Files (x86)\VMware\VMware VIX\vmrun.exe" -T ws -gu Administrator -gp password
SET VMX="C:\Users\whoever\Documents\Virtual Machines\Windows\Windows.vmx"
SET GUEST_COMSPEC="C:\WINDOWS\system32\cmd.exe"

REM Tried to do this in one line, but couldn't figure out the quoting.
%VMRUN% CreateTempfileInGuest %VMX% >%TEMP%\GuestTmp.txt || GOTO :CantCreate
FOR /F "delims=;" %%F IN ( %TEMP%\GuestTmp.txt ) DO SET GTEMP=%%F

REM The batch file is a one-liner that echos the variable into a file.
REM It could be generated dynamically and copied to the guest
REM but I didn't want to complicate things any further.
%VMRUN% runProgramInGuest %VMX% %GUEST_COMSPEC% "/c C:\echo-ProgramFiles.bat %GTEMP%"
%VMRUN% CopyFileFromGuestToHost %VMX% %GTEMP% %TEMP%\GuestOut.txt
%VMRUN% DeleteFileInGuest %VMX% %GTEMP%

REM Do something with the result and delete the temp files.
TYPE %TEMP%\GuestOut.txt
DEL %TEMP%\GuestOut.txt %TEMP%\GuestTmp.txt
GOTO :EOF

:CantCreate
REM Provide details on any problems.
TYPE %TEMP%\GuestTmp.txt 1>&2
DEL %TEMP%\GuestTmp.txt
EXIT 100

And here's the batch file on the guest host. As you can see, it's pretty simple. I couldn't get redirection to work in runProgramInGuest (probably didn't experiment enough) so I just pass the file as a command line argument.

@echo %PROGRAMFILES% >%1