Skip to content

Instantly share code, notes, and snippets.

@noseratio
Last active May 2, 2023 11:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noseratio/843bb4d9c410c42081fac9d8b7a33b5e to your computer and use it in GitHub Desktop.
Save noseratio/843bb4d9c410c42081fac9d8b7a33b5e to your computer and use it in GitHub Desktop.
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