如何通过Power Shell visual studio构建脚本无意中返回构建错误

时间:2018-02-14 07:07:37

标签: visual-studio powershell compilation

我有以下脚本用于逐个构建多个项目。但即使出现错误,此脚本也会继续运行。我想立即停止脚本并通知ures,其中一个项目无法构建存在编译器错误。即使可能准确通知无法编译的内容。请建议我怎么做?

param (
    [parameter(Mandatory=$false)]
    [String]$build 
)

function buildSas
{
    param
    (
        [parameter(Mandatory=$true)]
        [String] $path,

        [parameter(Mandatory=$false)]
        [bool] $clean = $false
    )
    process
    {
        $msBuildExe = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe';
        if(-not (Test-Path $msBuildExe))
        {
            Write-Host "MSBuild 15 is not installed, please install MSBuild 15 to use this build script." -ForegroundColor Red -BackgroundColor Yellow
            exit;  
        }

        if ($clean) {
            Write-Host "Cleaning $($path)" -foregroundcolor green
            & "$($msBuildExe)" "$($path)" /t:Clean /m:4
        }

        Write-Host "Building $($path)" -foregroundcolor green
        & "$($msBuildExe)" "$($path)" /t:Rebuild /m:4 /p:BuildInParallel=true  
    }
}

function buildVs
{
    Write-Host "Building all visual studio projects only." -ForegroundColor Magenta -BackgroundColor White;
    buildSas .\proj1.sln 
    buildSas .\proj2.sln
    buildSas .\proj3.sln
    buildSas .\proj4.sln
    # many project in list
    Write-Host "visual studio projects build done." -ForegroundColor Magenta -BackgroundColor White;
}

if($build -eq "vs")
{
    buildVs;
    return;
}

buildVs

1 个答案:

答案 0 :(得分:0)

如果msbuild.exe返回不同​​的退出代码我会添加函数以使用Start-Process启动msbuild。类似的东西:

$SuccessReturnCode=@(0,3010)
$ProcesTimeOut=3000
$buildparam="$($path) /t:Rebuild /m:4 /p:BuildInParallel=true"

Write-Log -message "Invoking: $myProcess $myArgs, process time-out:$ProcTimeOut seconds"
$process = (Start-Process -FilePath $msBuildExe  -windowstyle Hidden -ArgumentList $buildparam -PassThru)

    Try {
        $process | Wait-Process -Timeout $ProcTimeOut -ErrorAction Stop
        Write-Log -message "Completed with return code: $($process.ExitCode)"
            if ($SuccessReturnCode -notcontains $process.ExitCode) {
                Write-Log -level 'error' -message 'something went wrong'
                exit 255

            }

    }
    Catch {
        $ErrorMessage = $_.Exception.Message
        Write-Log -level 'error' -message "The error message was: <$($ErrorMessage)>"
        $pwid=$process.Id.ToString()
        taskkill.exe /PID $pwid /T /F
    }
相关问题