I have a simple PS script:
[CmdletBinding()]
param(
[Parameter()]
[String] $CodeCoverageXmlReportPath,
[Parameter()]
[Int32] $MinimumLineCoverage = 100
)
Write-Host $"MinimumLineCoverage = $MinimumLineCoverage"
Write-Host $"Reading CodeCoverage XML report: {$CodeCoverageXmlReportPath}"
### logic to calculate code coverage from report ###
if ($lineCoverageNum -lt $MinimumLineCoverage)
{
Write-Host $"Code coverage of this build ($lineCoverageNum) is below the expected value ($MinimumLineCoverage). Please add tests to validate new code."
exit 1
}
else
{
Write-Host $"Code coverage check (($lineCoverageNum)) for this build is at par with the ($MinimumLineCoverage). Great job!."
exit 0
}
These are the two ways I have tried to execute it from a .cmd file:
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -ExecutionPolicy Unrestricted -File ValidateCodeCoverage.ps1 -CodeCoverageXmlReportPath "%TestResultsDir%\Cobertura.xml" -MinimumLineCoverage 57
ECHO ErrorLevel=%errorlevel%
and
%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -
ExecutionPolicy Unrestricted -Command "& {.\ValidateCodeCoverage.ps1 -CodeCoverageXmlReportPath "%TestResultsDir%\Cobertura.xml" -MinimumLineCoverage 57; exit $LASTEXITCODE}"
ECHO ErrorLevel=%errorlevel%
In both these cases, checking the value of %errorlevel%
right after the .ps1 execution remains 0 no matter what. I tried tested it with always exiting with return code 1, using trap, throwing an exception, etc. The %errorlevel% in cmd file is always set to 0.
What could I be missing? :(