how do i get a variable out of powershell in jenkins declarative pipeline?

孤街醉人 提交于 2019-12-11 17:19:10

问题


        steps {
            script{
                env.StorysTested = ''

                try{
                    powershell('''
                     //some code here
                     foreach ( $item in $Comments ) 
                     {  
                     //some code here
                     //assigning a new value to StoryTested env variable
                     $env:StorysTested = "some value"   
                     }
                     //below line works fine and displays the value
                     Write-Output "Stories tested : $env:StorysTested"
                     ''')
                     //below null value is displayed for StorysTested`` 
                     echo " From Grrovy : ${env.StorysTested}"
                    }
                    catch(err)
                    {
                     throw err
                    }
           }

I am using a jenkins declarative pipeline. In the above code i m trying to use the value of $env:StorysTested in groovy which was assigned in powershell. Is there any way i can retain a variable value that was assigned in powershell, after the powershell execution is over. storing it in env variable was one way i thought of but clearly that didnt work.


回答1:


If you set an environment variable using $env:StorysTested = "some value", this variable is stored for the powershell process and is not permanent or visible outside this process.

To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method:

[Environment]::SetEnvironmentVariable("StorysTested", "some value", "User")

or

[Environment]::SetEnvironmentVariable("StorysTested", "some value", "Machine")

To delete from within PowerShell, you use the same .NET method and assign a $null value to the variable like this:

[Environment]::SetEnvironmentVariable("StorysTested",$null,"User")  # or "Machine" of course

Hope that helps



来源:https://stackoverflow.com/questions/55075193/how-do-i-get-a-variable-out-of-powershell-in-jenkins-declarative-pipeline

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