Is there a way to pass an object such as a hashtable to a powershell script file via command line?
This is my code:
Param(
[hashtable]$lookupTable = @{}
)
I tried this:
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File D:\script.ps1 @{APIKey="Uz9tkNhB9KJJnOB-LUuVIA"}
@{APIKey="Uz9tkNhB9KJJnOB-LUuVIA"}
being the hashtable parameter.
Error:
D:\script.ps1 : Cannot process argument transformation on parameter 'lookupTable'.
Cannot convert the "@{APIKey=Uz9tkNhB9KJJnOB-LUuVIA}" value of type "System.String" to type "System.Collections.Hashtable".
+ CategoryInfo : InvalidData: (:) [script.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,script.ps1
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:
- Script File
- Script execution mode ["Put script into PowerShell stdin with "-Command -" arguments" and "Execute .ps1 script with "-File" argument"].
- Additional command line parameters.
- 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:
powershell.exe -NonInteractive [commandline params] -ExecutionPolicy ByPass -Command - < [script file]
hence:
powershell.exe -NonInteractive @{ APIKey = 'Uz9tkNhB9KJJnOB-LUuVIA'} -ExecutionPolicy ByPass -Command - < D:\BuildAgent-02\work\2204bf4ff5f01dd3\scripts\script.ps1
This is the format teamcity is using to execute the script in -File mode:
powershell.exe -NonInteractive [commandline params] -ExecutionPolicy ByPass -File [script file] [script arguments]
hence when i use script params:
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File D:\BuildAgent-02\work\2204bf4ff5f01dd3\scripts\script.ps1 @{ APIKey = 'Uz9tkNhB9KJJnOB-LUuVIA'}
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?