Powershell脚本文件参数非字符串化

7
有没有办法通过命令行将对象(例如哈希表)传递给PowerShell脚本文件?
这是我的代码:
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 允许的输入方式包括:

  1. 脚本文件
  2. 脚本执行模式("使用“-Command -”参数将脚本放入PowerShell标准输入中"和"使用“-File”参数执行 .ps1 脚本")。
  3. 附加命令行参数。
  4. 脚本参数(仅在选择“使用“-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来序列化参数?
3个回答

6

TeamCity的PowerShell构建步骤允许您直接执行脚本文件或执行输入到构建步骤定义编辑器中的PowerShell源代码。

当从文件执行脚本时,哈希表参数无法正确传递,但是当执行PowerShell源代码时,一切都按预期工作。

然而,如果您要运行的脚本实际上在文件中,您可以通过输入到构建步骤定义中的PowerShell源代码来执行脚本文件:

  1. In the Script field, choose Source

  2. Enter a minimal PowerShell script to execute the script file into the Script source field, for example:

    &'%system.teamcity.build.checkoutDir%\BuildScripts\SomeScript.ps1' -MyArrayOfHashTablesParameter @{SomeParam1='val1';SomeParam2='val2'},@{SomeParam1='val1';SomeParam2='val2'}
    
上述PowerShell代码片段在构建期间执行从版本控制检出的脚本文件。为此,使用TeamCity变量system.teamcity.build.checkoutDir构造与检出目录相关的路径。 &''用于确保即使脚本文件的路径中包含空格,代码片段也能正常工作。

3

一种选择是修改脚本以使用[string[]]作为参数来接收键值对,然后在脚本中使用ConvertFrom-StringData将其转换为哈希表:

$script = {
param ( [string[]]$lookuplist )
$lookupTable = ConvertFrom-StringData ($lookuplist | out-string)
$lookupTable
}

&$script 'APIKey=Uz9tkNhB9KJJnOB-LUuVIA','APIKey2=Uz9tkNhB9KJJnOB-LUuVIA'


Name                           Value                                                         
----                           -----                                                         
APIKey                         Uz9tkNhB9KJJnOB-LUuVIA                                        
APIKey2                        Uz9tkNhB9KJJnOB-LUuVIA 

2

转而使用-Command参数:

powershell.exe -NonInteractive -ExecutionPolicy ByPass -Command "& {D:\script.ps1 @{APIKey='Uz9tkNhB9KJJnOB-LUuVIA'}}"

PowerShell.exe用法的关键部分:

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

谢谢您的回复,我尝试了那个方法,但不幸的是,命令构建和执行的格式有一些限制,我已经更新了我的问题。 - Shawn Mclean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接