Creating a PowerShell [hashtable] from a Jenkins text parameter

In Jenkins parameters are funky. In fact, the checkbox for “This build is parameterized” is pretty much a lie from a PowerShell standpoint. What is should say is “This build has environment variables created for it” because that is what it does. When you create a string parameter in a Jenkins job and give it a name of “rootPath” with a value “C:\”, what you get is an environment variable that you need to get at via $env:rootPath. Another problem with “This build is parameterized” is the limited data types you can create… which I recently found out when I wrote a script that used a [hashtable] param and I wanted to host it in Jenkins.  The trick? Use a “Text Parameter” and learn to love ConvertFrom-StringData.

As an example, say I want a $projects hashtable with the title of the project as the key and the variant I’m building as the value… I create a Text Parameter in Jenkins like so:

text_param_hash

Then in the PowerShell script I cast the $projects variable as a hashtable and give it a default vaule that puts the projects environment variable into it:

param(
[hashtable]$projects = $(ConvertFrom-StringData -StringData $env:projects)
)

Write-Output $projects

And then you get yourself a nice hashtable 🙂

[PowerShellTesting] $ powershell.exe "& 'C:\Windows\TEMP\hudson6969899475438348796.ps1'"

Name Value
---- -----
SQL DevInt.Q2.2015.SQL
Binaries DevInt.Q2.2015.Binaries
JBoss DevInt.Q2.2015.Java
ColdFusion DevInt.Q2.2015.CF

Finished: SUCCESS

Advertisements
This entry was posted in DevOps and tagged , , . Bookmark the permalink.

One Response to Creating a PowerShell [hashtable] from a Jenkins text parameter

  1. Pingback: Creating a PowerShell [hashtable] from a Jenkins text parameter | Jenkins Advanced

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