问题描述
每次关闭powershell会话或窗口时,在其中创建的所有变量都会被破坏.是否有任何命令或方法来创建即使关闭会话也不会破坏的变量.我知道使用脚本可以工作,但是我的情况是我的jenkins阶段打开一个窗口并创建一个变量,然后关闭该阶段以关闭会话,在下一阶段将打开新的会话,但所有初始变量都会丢失.我尝试使用jenkins环境变量,但即使它在powershell脚本中已更新,但在下一个会话中它又再次获得了相同的变量.
Every time I close the powershell session or window all the variable created in there is destroyed. Is there any command or way to create a variable which does not gets destoryed evenafter the session is closed.I know using script works but my situation is that my jenkins stage opens one window and creates a variable and the stage is closed which closes the session and in the next stage the new session is opened but all the initial variables are lost. I tried using jenkins environment variable but even though it is updated inside the powershell script but in next session it again gets same variable back.
推荐答案
作为会话1的示例,我们将变量设置为1.
As an example Session 1 our variable is set to a value of 1.
$a = 1
$a | export-clixml -path c:\temp\a.xml
现在关闭会话,然后在重新启动Powershell或新启动的会话后运行此代码.
now close the session then run this code after you relaunch Powershell or a newly started session.
if(test-path c:\temp\a.xml)
{
$a = import-clixml -Path c:\temp\a.xml
}
$a
1
这篇关于如何使用Jenkinsfile将Powershell变量从一个会话传递到另一个会话或从一个阶段传递到另一个阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!