The common problem is finding the installation location when we know what version we are looking for. Since here you are also asking about different versions, I think you have no right way and you will need to use one of the options.
Declaration by machine
Script arguments
As Lance Li-MSFT said in his answer, you can create a script that accepts the paths as parameters, and decides which one to use.
I think the disadvantage of this solution is that it is only suitable for the CI process, and it is difficult to run it in the development machine. And you still have to find the path in each machine.
Environment variables
Another option is to save the installation paths to environment variables and use them in the script.
The environment variables must be set once when installing Visual Studio or when creating the VM, and the script will access them as much as it wishes.
We still need to define the paths as environment variables in the development machine. If you have a lot of developers - it means a lot of emails.
Declaration by the script
Because there will not be an infinite list of installation paths, I would recommend searching for an existing path while running the script.
It does seem a little bad when you look at the file, but remember that the list is final and defined in one place, and there are no disadvantages that we have described in the other options.
Examples
Here is an example of finding Developer Command Prompt For VS 17, for development machines (Enterprise
, Professional
) and CI machines (BuildTools
).
By the way, it adds to the environment variables all kinds of important things, such as path to msbuild
and so on.
.bat
:
@echo off
if NOT DEFINED VSINSTALLDIR goto SetVars
echo VSINSTALLDIR already set to %VSINSTALLDIR%
goto :eof
:SetVars
if EXIST "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise" goto :Enterprise
if EXIST "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional" goto :Professional
if EXIST "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools" goto :Docker
echo VS2017 is not installed on this machine
goto :eof
:Enterprise
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\enterprise\common7\tools\VsDevCmd.bat"
goto :eof
:Docker
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\common7\tools\VsDevCmd.bat"
goto :eof
:Professional
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\common7\tools\VsDevCmd.bat"
:eof
msbuild
script:
<VsDevCmd Condition="Exists('C:\Program Files (x86)\Microsoft Visual Studio\2017\enterprise\common7\tools\VsDevCmd.bat')">
"C:\Program Files (x86)\Microsoft Visual Studio\2017\enterprise\common7\tools\VsDevCmd.bat"</VsDevCmd>
<VsDevCmd Condition="Exists('C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\common7\tools\VsDevCmd.bat')">
"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\common7\tools\VsDevCmd.bat"</VsDevCmd>
<VsDevCmd Condition="Exists('C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\common7\tools\VsDevCmd.bat')">
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\common7\tools\VsDevCmd.bat"</VsDevCmd>