Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables with batch file lauched by Powershell script

I have a batch script called SET_ENV.bat which contains environment variables that are used by other batch scripts. Currently this SET_ENV.bat is lauched by existing batch scripts.

Now I have a need to use Powershell script and I would like to launch the same SET_ENV.bat. I managed to do this using:

cmd.exe /c ..\..\SET_ENV.bat

I know that the batch file was run because it contained an echo

echo *** Set the environment variables for the processes ***

But after looking at the environment variables, I can see that none of them have been updated. Is there something that is preventing me from updating environment variables with Powershell + batch file combo?

I have tried SET_ENV.bat directly from command line and it works. I have also tried Start-Process cmdlet with "-Verb runAs" but that didn't do any good.

like image 621
masaz Avatar asked Feb 28 '18 10:02

masaz


1 Answers

Launching PowerShell again at the end of the batch commands will keep every environment variable so far.

My use case was: set up Anaconda environment, set up MSVC environment, continue with that. Problem is both Anaconda and MSCV have a separate batch script that initialises the env.

The following command starting from PowerShell will:

  • initialise Anaconda
  • initialise MSVC
  • re-launch PowerShell
cmd.exe "/K" '%USERPROFILE%\apps\anaconda3\Scripts\activate.bat %USERPROFILE%\apps\anaconda3 && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" && powershell'

Just swap the paths with what you need. Note that if the path contains spaces in needs to be inside double quotes ".

Breaking down the call above:

  • cmd.exe "/K": call cmd and do not exit after the commands finish executing /K

The rest is the full command, it is wrapped in single quotes '.

  • %USERPROFILE%\apps\anaconda3\Scripts\activate.bat %USERPROFILE%\apps\anaconda3: calls activate.bat with parameter ...\anaconda3
  • && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat": && and if the previous command didn't fail, run the MSVC vars setup file. This is wrapped in " as it has spaces in it.
  • && powershell: finally run PowerShell. This will now contain all environment variables from the ones above.

Just adding a better way of doing the aforementioned setup: using Anaconda's PowerShell init script to actually get it to display the environment name on the prompt. I won't break down this as it's just a modified command above.

cmd.exe "/K" '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" && powershell -noexit -command "& ''~\apps\anaconda3\shell\condabin\conda-hook.ps1'' ; conda activate ''~\apps\anaconda3'' "'

Note that the single quotes in the powershell call are all doubled up to escape them

like image 142
dtasev Avatar answered Sep 27 '22 23:09

dtasev