3

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

Therefore I need it as environment variable. This tutorial shows that I have to create the env.properties file which can be used. But I am not able to create one 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 the creation of the file seems to get bypassed. Is there something wrong?

I also tried:

Write-Host $projectCP > env.properties

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

If I execute the command directly in my PowerShell IDE everything works fine and the properties file gets created in my home directory.

4
  • 1
    Are you allowed to create the file in the directory? -> Permission issue? Commented May 10, 2017 at 4:42
  • The logged in jenkins user has full admin permissions. I forgot to say, there is no error message Commented May 10, 2017 at 4:44
  • 1
    Add -Verbose to Out-File. Also check $Error. Beforehand calling Out-File with -Verbose perform $Error.Clear(). Maybe you'll some extra information ... Commented May 10, 2017 at 4:45
  • There is no error after executing this line.. Commented May 10, 2017 at 4:57

2 Answers 2

2

I solved it. I have absolutely no idea why, but if I perform the command "CPName_VAR=$projectCP" | Out-File env.properties -Encoding ASCII at the very end of my PowerShell script, everything works fine and my file gets created.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you getting an error when it runs Out-File? If so, what's the error? If not, is the env.properties an already 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 is listed on your prompt at the time the command is run, it defaults to your user profile path [i.e. C:\Users\rkimble]).

If it was created, state the full path to the intended destination instead of just the file name (this should be default practice anyway, especially when running commands inside jobs).

If the file DOES already exist and you'd like to completely overwrite it with just that string, then add the -Force flag to your Out-File command:

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

If the file DOES already exist and you'd like to add that string to the end of it, then add the -Append flag to your Out-File command instead:

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

1 Comment

There is no specific error (I added some lines to my question) I searched the whole pc (The job runs on a slave, And also the jenkins master server, but there is definetly no file created

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.