views:

20

answers:

0

I'm creating a batch file to execute my psake builds while integrating with teamcity and the TFS Powershell commandlets from TFPT and have come up with the following:

@ECHO OFF

SET COMMAND_TO_EXECUTE=
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% "& {
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% Import-Module '..\tools\psake\psake.psm1';
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% try
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% {
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% invoke-psake -framework '4.0' -taskList %1 -properties @{config='%2'}
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% };
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% catch
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% {
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% . ..\tools\psake\teamcity.ps1; 
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% if(![string]::IsNullOrEmpty($env:TEAMCITY_VERSION))
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% {TeamCity-ReportBuildStatus -status 'FAILURE' -text $_}
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% else 
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% {Write-Host ERROR: $_ -ForegroundColor RED}; 
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% exit $Error.Count;
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% };
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% finally {remove-module psake};
SET COMMAND_TO_EXECUTE=%COMMAND_TO_EXECUTE% };"

echo Build command is %COMMAND_TO_EXECUTE%

IF %PROCESSOR_ARCHITECTURE% == x86 (
powershell -PSConsoleFile "%TFSPowerToolDir%tfshell.psc1" -Version "2.0" -NoProfile -ExecutionPolicy unrestricted -Command %COMMAND_TO_EXECUTE%
) ELSE C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "%TFSPowerToolDir%tfshell.psc1" -Version "2.0" -NoProfile -ExecutionPolicy unrestricted -Command %COMMAND_TO_EXECUTE%

I was having problems with builds not failing in TeamCity when errors occur due to Powershell never exiting with a non-zero exit code. So what I'm attempting to do in the script above is to catch any terminating errors from the Invoke-Psake command, report the error to teamcity and exit the process with a non-zero code equal to the number of errors that had occurred. As far as I can tell the script seems fine. Running the resulting command on it's own seems to work fine. However running it as part of the batch file above with the command assigned to the -Command argument of the powershell executable it fails with the error:

Missing expression after unary operator '-'

I can't see why that should be especially since running the command on it's own seems to work fine. Can anyone shed any light on the matter? It's probably a syntax error somewhere but I can't figure it out.