简体   繁体   English

如何将字符串数组传递给ps1文件?

[英]How to pass string array to ps1 file?

I'm using a Jenkins pipeline job. 我正在使用詹金斯管道作业。 My requirement is to get all the commit messages for the job and pass it to the .ps1 file from Jenkins pipeline file and then from the .ps1 file to a cake file. 我的要求是获取作业的所有提交消息,并将其从Jenkins管道文件传递到.ps1文件,然后再从.ps1文件传递到蛋糕文件。

I am using a string list to get all commit messages and later convert it to an array. 我正在使用字符串列表来获取所有提交消息,然后将其转换为数组。

Now, I need to pass this array value to the ps1 file from Jenkins pipeline file. 现在,我需要将此数组值从Jenkins管道文件传递到ps1文件。 I tried to pass like as a string in Jenkins pipeline file. 我试图像Jenkins管道文件中的字符串一样传递。 It throws an exception. 它引发异常。 Could anyone please let me know is this possible to pass array value to the .ps1 file from Jenkins file? 有人可以让我知道是否可以将数组值从Jenkins文件传递到.ps1文件?

Jenkins file content: Jenkins文件内容:

bat 'powershell.exe -ExecutionPolicy ByPass -File build.ps1 -Script build.cake -Target build -arrayContent '+"${arrayContent}"

PS1 file content: PS1文件内容:

Invoke-Expression "& `"$CAKE_EXE`" `"$Script`" -target=`"$Target`" arrayContent=`"$arrayContent`" -verbosity=`"$Verbosity`" $UseMono $UseDryRun $UseExperimental $ScriptArgs"

I'm declaring the array as [string[]]$arrayContent = @() in the .ps1 file. 我在.ps1文件中将数组声明为[string[]]$arrayContent = @()

Cake file content: 蛋糕文件内容:

var arrayContent = Argument<string[]>("arrayContent");

PowerShell arrays don't work outside PowerShell. PowerShell阵列无法在PowerShell外部运行。 You could pass the values as a single delimiter-separated string and split that string the PowerShell script: 您可以将值作为单个定界符分隔的字符串传递,然后将该字符串拆分为PowerShell脚本:

Param(
    [Parameter(Mandatory=$true)]
    [string]$ArrayContent,
    ...
)

$stringArray = $ArrayContent -split ','
...
powershell.exe ... -ArrayContent "foo,bar,baz"

Or pass each strings as an individual unnamed argument, so that they're collected in the automatic variable $args : 或者将每个字符串作为一个单独的未命名参数传递,以便将它们收集在自动变量$args

[CmdletBinding()]
Param(
    ...
)

$stringArray = $args
...
powershell.exe ... "foo" "bar" "baz"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM