gpt4 book ai didi

Powershell 脚本文件参数非字符串

转载 作者:行者123 更新时间:2023-12-04 11:07:00 26 4
gpt4 key购买 nike

有没有办法通过命令行将对象(例如哈希表)传递给 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 允许的输入是:
  • 脚本文件
  • 脚本执行模式 [“使用“-Command -”参数将脚本放入 PowerShell stdin”和“使用“-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 来序列化参数吗?

    最佳答案

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

    从文件执行脚本时,哈希表参数未正确传递,但在执行 PowerShell 源代码时,一切正常。

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

  • Script字段,选择 Source
  • 输入一个最小的 PowerShell 脚本来执行脚本文件到 Script source字段,例如:
    &'%system.teamcity.build.checkoutDir%\BuildScripts\SomeScript.ps1' -MyArrayOfHashTablesParameter @{SomeParam1='val1';SomeParam2='val2'},@{SomeParam1='val1';SomeParam2='val2'}

  • 上面的 PowerShell 片段从构建期间从版本控制 check out 的脚本文件中执行脚本。为此,TeamCity 变量 system.teamcity.build.checkoutDir用于构造相对于结帐目录的路径。
    &''即使脚本文件的路径包含空格,脚本周围的内容也用于确保代码段有效。

    关于Powershell 脚本文件参数非字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21653898/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com