Powershell script file parameters non-string

Question:

Is there a way to pass an object such as a hashtable to a powershell script file via command line?

This is my code:

I tried this:

@{APIKey="Uz9tkNhB9KJJnOB-LUuVIA"} being the hashtable parameter.

Error:

Based on the error, its interpreting the parameter as a string. I am also passing this parameter through teamcity, which only accepts parameters directly and passes it to a command line shown above.

Is there anything I can do to the parameter to tell powershell that it is an object of type hashtable?

PS.

The inputs allowed by teamcity are:

  1. Script File
  2. Script execution mode [“Put script into PowerShell stdin with “-Command -” arguments” and “Execute .ps1 script with “-File” argument”].
  3. Additional command line parameters.
  4. Script arguments (enabled if “Execute .ps1 with -File argument” is selected)

This is the format teamcity is using to execute the script in -Command mode:

hence:

This is the format teamcity is using to execute the script in -File mode:

hence when i use script params:

Is there anyway to work around this format that teamcity is using? For eg. under script arguments, could I do -Command there to serialize the params?

Answer:

One option might be to modify the script to take that argument as [string[]], giving it key-value pairs in the arguments and then turn that into a hash table using ConvertFrom-StringData in the script:

Source:

Powershell script file parameters non-string by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply