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:
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:
[hashtable]$projects = $(ConvertFrom-StringData -StringData $env:projects)
And then you get yourself a nice hashtable 🙂
[PowerShellTesting] $ powershell.exe "& 'C:\Windows\TEMP\hudson6969899475438348796.ps1'"