private/Find-MsBuild.psm1

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
function Find-MsBuild
{
    try 
    {
        $msbuildCommand = Get-Command msbuild 2> $null
        if ($?)
        {
            return $msbuildCommand.Source
        }
    }
    catch
    {
        Write-Debug "MSBuild not found on PATH. Trying elsewhere."
    }

    Install-Module VSSetup -Scope CurrentUser -Force
    $instance = Get-VSSetupInstance -All -Prerelease | Select-VSSetupInstance -Require 'Microsoft.Component.MSBuild' -Latest
    $installDir = $instance.installationPath
    $msBuild = $installDir + '\MSBuild\Current\Bin\MSBuild.exe' # VS2019
    if (![System.IO.File]::Exists($msBuild))
    {
        $msBuild = $installDir + '\MSBuild\15.0\Bin\MSBuild.exe' # VS2017
        if (![System.IO.File]::Exists($msBuild))
        {
            ThrowError "MSBuild doesn't exist. Exit."
        }
    }

    return $msBuild
}

Export-ModuleMember -Function Find-MsBuild