Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在我的Powershell脚本中使用Jenkins变量_Powershell_Jenkins - Fatal编程技术网

如何在我的Powershell脚本中使用Jenkins变量

如何在我的Powershell脚本中使用Jenkins变量,powershell,jenkins,Powershell,Jenkins,我已经通过PowerShell编写了一个简单的脚本来收集一些文件并将它们压缩到一个文件夹中,我们称之为script.ps1。我想让Jenkins每次进行新构建时都运行脚本,但是我也希望zip文件的名称是构建编号 如何在PowerShell中创建一个变量,即Jenkins的当前版本号?现在我正在调用配置的executeshell部分中的Script.ps1 我不熟悉詹金斯,但我相信构建编号是一个环境变量 要在PowerShell中访问它,请使用$env:BUILD\u NUMBER 例如,如果使用

我已经通过
PowerShell
编写了一个简单的脚本来收集一些文件并将它们压缩到一个文件夹中,我们称之为
script.ps1
。我想让Jenkins每次进行新构建时都运行脚本,但是我也希望zip文件的名称是
构建编号


如何在
PowerShell
中创建一个变量,即Jenkins的当前版本号?现在我正在调用配置的executeshell部分中的
Script.ps1

我不熟悉詹金斯,但我相信
构建编号
是一个环境变量

要在PowerShell中访问它,请使用
$env:BUILD\u NUMBER

例如,如果使用7-zip

7z.exe a "$env:BUILD_NUMBER.zip" C:\Build\Path 

您可以将参数添加到
脚本.ps1
。只需在脚本顶部使用
Param

Param( $BuildNumber ) #must be first statement in script
# your current script goes here

然后,您可以调用脚本作为Jenkins的参数传递
BUILD\u NUMBER
。有关使用参数调用Powershell脚本的信息,请参阅。

您也可以使用Powershell插件,请参阅

它允许您直接在Jenkins的文本框中输入PowerShell命令

然后您可以使用可用的环境变量(参见插件的docu)。请注意,访问非常神秘(就我而言):


还要注意双引号的转义。我花了很长时间:)

我刚在詹金斯试过这个,效果非常好。塔克斯!谢谢我不知道为什么您的解决方案被标记为绿色复选标记。这才是真正的解决办法。
(Get-Content ./Sources/SlmMachineControl/GUI/Project1.rc).replace("`"FileVersion`", `"1.0.0.0`"" ,"`"FileVersion`" `"2.3.$($env:BUILD_NUMBER).0`"") | Set-Content ./Sources/SlmMachineControl/GUI/Project1.rc