Running a build script after calling vcvarsall.bat from powershell

Question:

I am attempting to run the Visual Studio (developer cmd prompt) environmental variable setup batch file followed by a build script from within a Powershell script as follows:

cmd /v:on/k "(""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64_x86 && C:\buildscript.cmd --build-options)"

It appears however that the environmental settings established by vcvarsall.bat are not retained for the build script call. i.e. no default compiler setup, etc.

Is the /v:on combined with the /k switch not actually utilizing the same cmd session and properly delaying environmental variable expansion? Perhaps the approach is wrong …

Answer:

The problem is that when you run cmd.exe to run a batch file, the variables are set in that instance of cmd.exe, but they disappear after that instance terminates.

To work around this problem, you can use the Invoke-CmdScript function in this article:

Windows IT Pro: Take Charge of Environment Variables in PowerShell

The function is as follows:

…or whatever you need.

The article also presents a couple of functions that let you easily save and restore environment variables.

Source:

Running a build script after calling vcvarsall.bat from powershell by licensed under CC BY-SA | With most appropriate answer!

| foreach-object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
set-item Env:$varName $varValue
}
}
You could add this function to your PowerShell profile or use it as a script file.

Once you have defined the function, you can run your commands:

…or whatever you need.

The article also presents a couple of functions that let you easily save and restore environment variables.

Source:

Running a build script after calling vcvarsall.bat from powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply