Team City PowerShell and Parameters

There are times when its really annoying, you get technology x up and running on your development machine and then try to integrate it into the wider enterprise system and fall at what is really the first hurdle, this has happened to me again, hence this post about what on the surface is a simple task, but has taken me some days to sort out.

The task:
Getting a PowerShell script to run within a Team City build where the PowerShell script needs a couple of parameters handed in.  told you it was simple! lets have a quick look at the script.

param(
    [parameter(Mandatory=$true)] [string]$arg1,
    [parameter(Mandatory=$true)] [string]$arg2,
    [parameter(Mandatory=$false)] [int]$arg3=2,
    [parameter(Mandatory=$false)] [int]$arg4=3
)

Function ParameterTest
{
    $total = $arg3+$arg4
    
    Write-Host $arg1
    Write-Host $arg2
    Write-Host $total
}

ParameterTest

Nothing scary, the important bit is the parameters at the top, these must be the first executable lines of the script and when the script is called the first two are mandatory so its off to team city to setup the build step.

teamcity blog picture

For me the thing to remember is to set the script execution mode to file, this shows the additional script arguments block where we can add the script arguments and there values, note we can use the team city environment variables here as usual.

This post is mainly for the future me who can’t get the script to run in Team City, remember that power shell must have execution permissions set.

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Programming and tagged , . Bookmark the permalink.

11 Responses to Team City PowerShell and Parameters

  1. zentoid says:

    And here I have been doing it as “Source code” and just having the script updated with Team City parameters before execution.

    Good one Dr B

  2. Colin McNulty says:

    Thanks mate, that got it working… right up until I tried passing in a parameter that had a space in.

    When I pass the parameter: COLIN1 using this in the Script arguments:

    arg1=”COLIN1″

    • Colin McNulty says:

      [Oops, continued…]

      Then it works fine, and looking at the build log I see this at the end of the powershell call:

      … arg1=”COLIN1″

      BUT, if I try passing in this parameter, putting a space before the 1

      arg1=”COLIN 1″

      Then the powershell line fails. It fails because this is what it puts at the end of the powershell call:

      … “arg1=”COLIN 1″”

      Basically it seems to be detecting the space, and wrapping the whole thing in an extra set of quotes. D’oh!

      Any thought gratefully received.

  3. bastienJS says:

    The File Argument seems to be removed from TeamCity 9 ? I can not find it anymore!

  4. tigertom22 says:

    Thanks, very helpful.

  5. Anonymous says:

    Hi. Passing arguments like you arg1=”hello” will init arg1 with value “arg1=hello”. If you want pure value of variable (without variable name) then you have to use next syntax in arguments textarea: http://www.ralbu.com/teamcity-build-parameters-for-powershell

  6. Leszek says:

    how to pass PASSWORD securely ?

  7. szumigalski says:

    VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It demonstrates that you have the knowledge and abilities required to leverage best practices to provide a scalable and reliable Business Mobility platform for your organization. Some of the subjects involve: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Szumigalski.com was created by Sebastian to propagate his interest for PowerShell & VMWare. Sebastian is an IT skilled professional employed in Singapore over 15 years who is at all times looking for brand-new technological innovation to develop his technical competencies & knowledge. Ever since, Sebastian has become a member of PowerShell User Group & VMWare VMug group, and has been engaging in every one of the activities held in Singapore. This website will disclose precisely how Sebastian are able to speed up his every day undertakings utilizing PowerShell. You can find study guides readily available for the VCAP6-DTM test, which were personally published by Sebastian. Sebastian is licensed with VCAP6-DTM, and is strong with virtualization & server maintenance from 4 years experience of automation. The interest in VMWare knowledgable admins and technicians are ever-increasing in the present tech market place. Start to learn more details on PowerShell & VMWare with Szumigalski.com!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s