Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
find Visual Studio and run its VsDevCmd.bat, then $buildCommand
<#
.Synopsis
Find Visual Studio and run its VsDevCmd.bat, then run a build command.
.Example
powershell -f _invoke-build.ps1 -buildCommand "msbuild ."
.Link
https://gist.github.com/noseratio/843bb4d9c410c42081fac9d8b7a33b5e
#>
#Requires -Version 5.0
# by @noseratio
param([Parameter(Mandatory = $true)] [string] $buildCommand)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
echo "Building via '$buildCommand' ..."
$vs_not_found = "Visual Studio hasn't been detected!"
$vswhere = "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe"
if (!(Test-Path $vswhere)) { throw $vs_not_found }
$vs_ide_path = $(& $vswhere -format value -property productPath)
if (!(Test-Path $vs_ide_path)) { throw $vs_not_found }
$vs_ide_folder = "$(Split-Path $vs_ide_path)"
$vs_dev_cmd = "$vs_ide_folder/../Tools/VsDevCmd.bat"
$invoke = "call ""$vs_dev_cmd"" && cd ""$pwd"" && $buildCommand"
# we need to run via CMD for proper propagation of Visual Studio environment vars
& $env:comspec /s /c ""$invoke""
if (!$?) { throw "Failed with error code: $LastExitCode" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.