Monday, June 20, 2016

TeamCity Build Agent path causes problems in PowerShell build runner

I was recently working with an installation of TeamCity that was installed into the Program Files directory when I discovered that none of my PowerShell scripts were working properly!

This was the error message I was receiving:

[Step 3/3] PowerShell Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
[21:09:28][Step 3/3] Working directory: D:\Program Files\TeamCity\buildAgent\work\2f8c6ee9aa79151b
[21:09:28][Step 3/3] PowerShell arguments: [-NonInteractive, -ExecutionPolicy, ByPass, -File, D:\Program Files\TeamCity\buildAgent\temp\buildTmp\powershell3015355379462820230.ps1, -SrcDir:"D:\Program Files\TeamCity\buildAgent\work\2f8c6ee9aa79151b\src", -DestDir:D:\Websites\MyWebsite]
[21:09:28][Step 3/3] D:\Program
[21:09:28][Step 3/3] Files\TeamCity\buildAgent\temp\buildTmp\powershell3015355379462820230.ps1 : A
[21:09:28][Step 3/3] positional parameter cannot be found that accepts argument
[21:09:28][Step 3/3] 'Files\TeamCity\buildAgent\work\2f8c6ee9aa79151b\src'.
[21:09:28][Step 3/3]     + CategoryInfo          : InvalidArgument: (:) [powershell3015355379462820
[21:09:28][Step 3/3]    230.ps1], ParentContainsErrorRecordException
[21:09:28][Step 3/3]     + FullyQualifiedErrorId : PositionalParameterNotFound,powershell3015355379
[21:09:28][Step 3/3]    462820230.ps1
[21:09:28][Step 3/3] 
[21:09:28][Step 3/3] Process exited with code 0

Well, as it turns out, this is a flaw/defect in the current version of TeamCity (v. 9.1.7 as of this writing) that does not escape the PowerShell paths correctly!

You can track the status of this issue here:

Of course, one workaround is to simply uninstall the TeamCity Build Agent into a path that does not contain spaces in the path. 

Another workaround was suggested by the TeamCity staff:
After further investigation, we have found, that if the parameters are passed to the script in the form -paramName:paramValue, TeamCity does not handle their escaping correctly.  For now, I suggest that in your script you use the format -paramName -paramValue (with space instead of :).   So the value of the 'script arguments' field will look like this: -SrcDir "\src" -DestDir %system.DestDir%

As for me, I chose to simply re-install the build agent into a standard directory such as C:\TeamCityBuildAgent, and then my PowerShell build runner worked just fine!

No comments:

Post a Comment