English 中文(简体)
Running PSAKE script from the command line
原标题:

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  ..	oolspsakepsake.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% . ..	oolspsake	eamcity.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:WindowsSysWOW64WindowsPowerShellv1.0powershell.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.

问题回答

Hmmm, think you need to do some magic with escape charaters. Any invocation of powershell I do from cmd I try to keep the formation as follows -

"powershell.exe -command & {. C:TempExample.ps1 -param1 hello} "

Your complication comes from the multi line cmd variable you are trying to build up, each line as the variable is expanded will remove any escape sequences you apply. I messed around a bit for you, but then thought.... why dont you just create .ps1 script with your script block in, saves any nasty escaping etc.

So Invoke-Build.ps1 becomes - Param($TaskList, $Config) Import-Module .. oolspsakepsake.psm1 try { invoke-psake -framework 4.0 -taskList $TaskList -properties @{config=$Config} } catch { . .. oolspsake eamcity.ps1; if(![string]::IsNullOrEmpty($env:TEAMCITY_VERSION)) { TeamCity-ReportBuildStatus -status FAILURE -text $_ } else { Write-Host ERROR: $_ -ForegroundColor RED } exit $Error.Count } finally {remove-module psake}

Then you can just dot source the script from cmd......

@echo OFF

SET COMMAND_TO_EXECUTE=Invoke-Build.ps1

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% -TaskList -Config}" ) ELSE ( C:WindowsSysWOW64WindowsPowerShellv1.0powershell.exe -PSConsoleFile "%TFSPowerToolDir%tfshell.psc1" -Version "2.0" -NoProfile -ExecutionPolicy unrestricted -Command "& {. %COMMAND_TO_EXECUTE% -TaskList -Config}"





相关问题
Why not use TFS as a build / CI solution?

Currently our build solution is set up using TFS + MS Build scripts. TFS is also being used as a CI server. I ve seen several posts on this site telling people about other CI solutions. Are there ...

Get files from TFS under Linux [closed]

is there a free (command line) tool for linux which with I can get all files from a TFS-Repository (no Check in / Check out required - only get actual version)?

upgrading tfs 2008 sp1 to use sql server 2008

I have an instance of tfs 2008 supported by sql server 2005. I want to change the sql server machine by doing a restore based move. I also want to change the version of sql server to 2008. I know ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

TFSReg in 2010 Beta 2?

does anybody know what is the equivalent of the TFSReg.exe command-line tool in 2010 Beta 2? I cannot find it anywhere, I searched the entire Program Files tree. Was it renamed? Moved? Replaced by ...

热门标签