For doing some automation on .Net it is needed to know path for msbuild.exe (or add it on path).

We would expect it to always be on same path.. yet it was found not to be so.

So… how to solve it?

there is some modules in PowerShell that proven to be very helpfully:

  • VSSetup
  • BuildUtils

Idea behind it is to get access to a method – Get-LatestMsbuildLocation – so we have a known path for it.

If wished to use on a module you may just have it like this:

function MsBuildModuleDependenciesCheck{
      param()

      if (Get-Module -ListAvailable -Name VSSetup) {
            Write-Host "Module VSSetup check" -ForegroundColor Green -BackgroundColor White
        } 
        else {
            Write-Host VSSetup "Module does not exist... installing"
            Install-Module VSSetup -Scope CurrentUser -Force
        }

        if (Get-Module -ListAvailable -Name BuildUtils) {
            Write-Host "Module BuildUtils check" -ForegroundColor Green -BackgroundColor White
        } 
        else {
            Write-Host BuildUtils "Module does not exist... installing"
            Install-Module BuildUtils -Scope CurrentUser -Force
        }
}

function BuildSolutionBinaries {
      param(
			$solutionPath,
            $destPath,
            $compilationMode   
      )

      $msbuildLocation = Get-LatestMsbuildLocation
      Write-Host "Compiling..." -ForegroundColor White -BackgroundColor Blue
      & $msbuildLocation $solutionPath /p:OutputPath=$destPath /nologo /nr:false  /p:platform="any cpu" /p:configuration="$compilationMode" /p:VisualStudioVersion="14.0" /verbosity:q  /clp:ErrorsOnly
}

Now just need to ensure modules are on your ssytem and you are good to go.

Enjoy!