I am trying to access a user environment variable on a virtual machine from a batch file running on it. This batch file is called from a batch file on the host machine which also sets the environment variable.
If I open a new command prompt in the virtual machine then I can get the value of this variable. But if I try to access it from the batch file on the virtual machine which is called from the batch file on the host machine I cannot access it.
Batch file on the host machine:
set BUILD=4732
"C:\Program Files (x86)\VMware\VMware VIX\vmrun" -T ws -gu PostBuild -gp ****** -h https://WIN-M7AT2VUR3HQ:8333/sdk writeVariable "E:\VM Testing\Windows 7.vmx" guestEnv BUILD %BUILD%"
"C:\Program Files (x86)\VMware\VMware VIX\vmrun" -T ws -gu PostBuild -gp ****** -h https://WIN-M7AT2VUR3HQ:8333/sdk runProgramInGuest "E:\VM Testing\Windows 7.vmx" -activeWindow -interactive "C:\Automation\CopylatestbuildAndInstall.bat"
pause
Is there some way to change the user account the batch file on the virtual machine is running as? So that it has access to the variable.
Notice that when you launch a process, such as command-line processor, enviroment variables are copied from parent process and each process works on its own environment variables.
Explorer.exe
and maybe other system processes copies system and user default environment variables to child processes.There is a method to set default system/user environment,
SETX
. This method changes default variables, not current ones.SET
change current, but not default.Getting to your problem:
It seams to me that your batch is writing default environment. This way you can access variable when you get a new process.
But it is not setting command line environment, so when you continue execution, you don't have new variables.
To workaround this, you have to get default environment variable to your current environment.
To do such you must get it from registry:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
for system variables,HKCU\Environment
for user.Next example will read
BUILD
default user variable from registry and set it in current batch: