forked from ge-flight-analytics/EmsApi.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find-MsBuild.ps1
34 lines (28 loc) · 1.34 KB
/
Find-MsBuild.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$msBuild = Get-Command "msbuild" -ErrorAction SilentlyContinue
if( $null -eq $msBuild ) {
if( $IsWindows -eq $false ) {
# This is powershell core, and we're not on windows.
return $null
}
$vsInfo = & $PSScriptRoot\vswhere.exe -latest -format json | ConvertFrom-Json
if( $vsInfo ) {
$msbuildDir = Join-Path $vsInfo.installationPath 'MSBuild'
$exe = Get-ChildItem $msbuildDir -Recurse -Force -Filter msbuild.exe | Select-Object -First 1 -ExpandProperty FullName
if( $exe ) {
$msBuild = Get-Command $exe
}
}
if( $null -eq $msBuild ) {
# Get the path from the registry, if possible. Enumerate the tools versions and take the highest one.
$versions = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions"
$highestVersion = $versions | Sort-Object @{ expression = { [int](Split-Path $_.Name -Leaf) }; Descending = $false } | Select-Object -Last 1
$buildToolsDir = $highestVersion.GetValue( "MsBuildToolsPath" )
$msBuildPath = Join-Path $buildToolsDir "msbuild.exe"
if( -not ( Test-Path $msBuildPath ) )
{
throw "Could not locate msbuild.exe on the PATH or using the MSBuildToolsPath in HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions"
}
$msBuild = Get-Command $msBuildPath
}
}
return $msBuild