Is there a way to access TeamCity system properties in a Powershell script? Is there a way to access TeamCity system properties in a Powershell script? powershell powershell

Is there a way to access TeamCity system properties in a Powershell script?


TeamCity will set up environment variables, such as build.number (you can see a list of these within TeamCity).

In Powershell you can access environment variables using the env "provider", e.g.

$env:PATH

TeamCity variables are accessible by replacing the . with a _, so the build.number variable can be accessed as

$env:build_number


As it says in the TeamCity documentation, the system parameters are passed to the build script runner, but not all build script runners know what to do with them. In the case of the Powershell script runner, when using a script file, they don't propagate down to your scripts.

It's occurred to me to write a psake-optimized build runner that does, but in the meantime you can do one of the following:

  • explicitly map any of the TeamCity build properties to script parameters using the parameter expansion that's available within the Script Source box. eg .\build.ps1 -someParam:%build.name%

  • use environment parameters, which can be accessed explicitly within PowerShell using $env:NAME_IN_TEAMCITY syntax, eg $env:TEAMCITY_VERSION, or looped over and pushed into variable scope

  • access the build properties file that TeamCity makes available during the build. The file is available at $env:TEAMCITY_BUILD_PROPERTIES_FILE, and if you load the XML version it's fairly easy to loop through and push them all into scope (though you do get everything as a string of course). I posted a gist on how to do this (https://gist.github.com/piers7/6432985). Or, if using Psake, modify the script above to return you a hashtable which you can pass directly to Psake's -properties argument.


It is posible. Here is example how to pass system properties into PSake script:

& .\psake.ps1 -parameters @{build_number=%build.number%; personal_build=%build.is.personal%}

If you don't use Psake, you can define your variables like this:

$build_number = %build.number%

The %build.number% part will be replaced with TeamCity provided data. I think, it works only in Source code script input mode.