Use PowerShell variable as environment variable

I want to use a variable in my PowerShell build step in Jenkins Freestyle job to start another job.

So I need this as an environment variable. This tutorial shows that I need to create an env.properties file that can be used. But I cannot create it inside my script. There is this part:

Write-Host "TEST1"
"CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII
Write-Host "TEST2"

      

The console prints out TEST1

and TEST2

, but creating a file seems to be bypassed. Something is wrong?

I also tried:

Write-Host $projectCP > env.properties

      

There is no mistake. But the envinject plugin (which I use to insert parameters from the properties file) throws an error at the end of the job because "there is no such file."

If I run the command directly in my PowerShell IDE everything works fine and a properties file is created in my home directory.

+2


source to share


2 answers


I solved it. I have no idea why, but if I "CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII

command "CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII

"CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII

at the very end of my PowerShell script, everything works fine and my file is generated.



+1


source


Are you getting an error on startup Out-File

? If so, what is the mistake? If not, is it a env.properties

pre-existing file?

Here's what I would check ... if the file doesn't already exist, check to see if it was created in your current working directory (whatever appears in your prompt while executing the command matches your user profile by default). path [ie C:\Users\rkimble

]).

If it was created, provide the full path to the intended destination, not just the filename (this should be the default practice anyway, especially when running commands inside jobs).

If the file already exists and you want to completely overwrite it with just this line, add a flag -Force

to your command Out-File

:



"CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII -Force

      

If the file already exists and you want to add this line to the end, add a flag -Append

to the command Out-File

:

"CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII -Append

      

0


source







All Articles