3

Is there any way i can read TeamCity user defined parameter values using PowerShell script.

Following is what I'm trying to do.

I have already created following parameters in the TeamCity build configuration

Variable Name           Variable Value
=============           ==============
variable1               ABC
variable2               XYZ

Following is my powershell script that I'm using in my PowerShell Buid Step:

    #Samplay array
    $Array = @("variable1","variable2","variable3","variable4","variable5")

    Foreach ($item in $Array)
    {
        $VariableValue = "%" + "$item" + "%"
        if ($VariableValue)
        {
            Write-Host "Found TeamCity Variable called '$item'. Corresponding variable value is '$VariableValue'"
        }
        else
        {
            Write-Host "There isn't any TeamCity variable called '$item3'. Aborting..."
            Exit -1
        }
    }

The expected output is:

Found TeamCity Variable called 'variable1'. Corresponding variable value is 'ABC'
Found TeamCity Variable called 'variable2'. Corresponding variable value is 'XYZ'
There isn't any TeamCity variable called 'variable3'. Aborting...

I thought I can access the TeamCity variables using %teamcityvariablename% format. But it seems this is not working. I dont want to hardcode each and every variables in script arguments. Because the variables will change always. Can someone please suggest me how can i use the TeamCity user defined parameters (Configuration Parameters) in PowerShell build step. Thanks in advance.

mahesh
  • 468
  • 2
  • 8
  • 25

1 Answers1

3

I've found a solution after much searching and testing. First, there doesn't seem to be any way to access Configuration parameter types from within a PowerShell script. If there is, I've found no documentation outlining this and no amount of experimenting has yielded a way to access these parameters. Therefore, if your parameters are Configuration Parameter types, you will need to convert them to environment parameters.

The environment parameters are all written as environment variables within the local execution scope of PowerShell. This means that if you need to access a TeamCity parameter like env.ConnectionString from within your PowerShell script, you can access it as you would any other environment variable. Example:

#TeamCity populated the env:ConnectionString before running the script.   
$ConnectionString = $env:ConnectionString 

Regarding System Parameter type parameters, I assume there is not a way to access these either but I could be wrong. I have not had to use system parameters, so I have not had to experiment with accessing these.

Please also note, we use actual PowerShell scripts with our project. Within the TeamCity PowerShell build step, there is an options to execute your PowerShell code as either a "File" or as "Source Code". I'm not sure if there is greater flexibility with the "Source Code" option since you are given a TeamCity text region to define the PowerShell code. They may parse your code before executing it in PowerShell but I have not tested this approach.

This information may be common knowledge but after much searching I was unable to find a straightforward answer to this issue. My personal source for this information is trial-and-error. If anyone had any additional information that outlines how to access System, or even Configuration, Parameters within an executed PowerShell script, please post below and I'll update this answer.

RLH
  • 15,230
  • 22
  • 98
  • 182