有没有办法通过命令行将对象(例如哈希表)传递给PowerShell脚本文件?
这是我的代码:
我尝试了这个:
因此:
这是TeamCity在“-File”模式下执行脚本所使用的格式:
因此,当我使用脚本参数时:
有没有办法绕过TeamCity使用的这种格式?例如,在“脚本参数”下,是否可以使用-Command来序列化参数?
这是我的代码:
Param(
[hashtable]$lookupTable = @{}
)
我尝试了这个:
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File D:\script.ps1 @{APIKey="Uz9tkNhB9KJJnOB-LUuVIA"}
@{APIKey="Uz9tkNhB9KJJnOB-LUuVIA"}
是哈希表参数。
错误:
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
根据错误信息,它将参数解释为字符串。我还通过Teamcity传递此参数,该工具仅接受直接传递的参数并将其传递给上面显示的命令行。
有什么方法可以将参数转换为PowerShell的哈希表对象类型吗?
附:
Teamcity 允许的输入方式包括:
- 脚本文件
- 脚本执行模式("使用“-Command -”参数将脚本放入PowerShell标准输入中"和"使用“-File”参数执行 .ps1 脚本")。
- 附加命令行参数。
- 脚本参数(仅在选择“使用“-File”参数执行 .ps1”选项时启用)
这是 Teamcity 在“-Command”模式下执行脚本所使用的格式:
powershell.exe -NonInteractive [commandline params] -ExecutionPolicy ByPass -Command - < [script file]
因此:
powershell.exe -NonInteractive @{ APIKey = 'Uz9tkNhB9KJJnOB-LUuVIA'} -ExecutionPolicy ByPass -Command - < D:\BuildAgent-02\work\2204bf4ff5f01dd3\scripts\script.ps1
这是TeamCity在“-File”模式下执行脚本所使用的格式:
powershell.exe -NonInteractive [commandline params] -ExecutionPolicy ByPass -File [script file] [script arguments]
因此,当我使用脚本参数时:
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File D:\BuildAgent-02\work\2204bf4ff5f01dd3\scripts\script.ps1 @{ APIKey = 'Uz9tkNhB9KJJnOB-LUuVIA'}
有没有办法绕过TeamCity使用的这种格式?例如,在“脚本参数”下,是否可以使用-Command来序列化参数?