How to pass powershell variable from one session to another or from one stage to another in terms of Jenkinsfile

只愿长相守 提交于 2021-02-11 12:29:37

问题


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:


As an example Session 1 our variable is set to a value of 1.

$a = 1
$a | export-clixml -path c:\temp\a.xml

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


来源:https://stackoverflow.com/questions/56637777/how-to-pass-powershell-variable-from-one-session-to-another-or-from-one-stage-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!