New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting Powershell variables between steps #375

Closed
MiguelAlho opened this Issue Aug 6, 2015 · 25 comments

Comments

Projects
None yet
@MiguelAlho

MiguelAlho commented Aug 6, 2015

Is it possible to define (environment or global) variables in a powershell build step, and be able to use it in another (powershell) step further ahead?

I've been trying to do this, for instance, in a step where a set a variable for the package version by running GitVersion, and then further on, after running the build and tests, tag my repo. The script where I run the tagging step can't get the value of the variable I previously set.

Any recomendations?

Thanks!

@chrisrpatterson

This comment has been minimized.

Show comment
Hide comment
@chrisrpatterson

chrisrpatterson Aug 6, 2015

Member

You can use the following command by simply writing it to standard out (Write-Host) in powershell

##vso[task.setvariable variable=testvar;]testvalue

This will make the variable available later in the build and it should be in your environment. We have a behavior change coming in the next deployment (Early September) that will late bind variables references in inputs so you could set a variable in one task and then use it in an input via the $(testvar) syntax and the replacement will happen as expected.

Member

chrisrpatterson commented Aug 6, 2015

You can use the following command by simply writing it to standard out (Write-Host) in powershell

##vso[task.setvariable variable=testvar;]testvalue

This will make the variable available later in the build and it should be in your environment. We have a behavior change coming in the next deployment (Early September) that will late bind variables references in inputs so you could set a variable in one task and then use it in an input via the $(testvar) syntax and the replacement will happen as expected.

@MiguelAlho

This comment has been minimized.

Show comment
Hide comment
@MiguelAlho

MiguelAlho Aug 11, 2015

thanks!

unfortunately, I'm using TFS 2015 and that suggestion doesn't seem to work or i'm completely doing it wrong. As a workaround, I'm persisting the values to file and reading from the file in the steps ahead. I'll change that when late binding becomes available!

MiguelAlho commented Aug 11, 2015

thanks!

unfortunately, I'm using TFS 2015 and that suggestion doesn't seem to work or i'm completely doing it wrong. As a workaround, I'm persisting the values to file and reading from the file in the steps ahead. I'll change that when late binding becomes available!

@adriaanthomas

This comment has been minimized.

Show comment
Hide comment
@adriaanthomas

adriaanthomas Sep 9, 2015

@chrisrpatterson is it possible to give an indication when exactly "Early September" is?

We'd like to use the behaviour change you mention to deploy to a site / slot specifically for a branch. We have a PowerShell script that determines the correct name and calls Write-Host to create a new variable, and would like to use this as input for our deployment task...

adriaanthomas commented Sep 9, 2015

@chrisrpatterson is it possible to give an indication when exactly "Early September" is?

We'd like to use the behaviour change you mention to deploy to a site / slot specifically for a branch. We have a PowerShell script that determines the correct name and calls Write-Host to create a new variable, and would like to use this as input for our deployment task...

@npenin

This comment has been minimized.

Show comment
Hide comment
@npenin

npenin Oct 28, 2015

@chrisrpatterson Will it happen in TFS on premise ?

npenin commented Oct 28, 2015

@chrisrpatterson Will it happen in TFS on premise ?

@chrisrpatterson

This comment has been minimized.

Show comment
Hide comment
@chrisrpatterson

chrisrpatterson Oct 28, 2015

Member

It is in 2015 Update 1

From: Nicolas Penin [mailto:notifications@github.com]
Sent: Wednesday, October 28, 2015 8:05 AM
To: Microsoft/vso-agent-tasks vso-agent-tasks@noreply.github.com
Cc: Chris Patterson Chris.Patterson@microsoft.com
Subject: Re: [vso-agent-tasks] Persisting Powershell variables between steps (#375)

@chrisrpattersonhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fchrisrpatterson&data=01%7c01%7cChris.Patterson%40microsoft.com%7c3caf80c8a50348d2a1dd08d2df900c93%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=TJ4ZLAHjjMfWAmd6wYqbGbRpFuMn3Ebu2mrsO801BJw%3d Will it happen in TFS on premise ?


Reply to this email directly or view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fMicrosoft%2fvso-agent-tasks%2fissues%2f375%23issuecomment-151823523&data=01%7c01%7cChris.Patterson%40microsoft.com%7c3caf80c8a50348d2a1dd08d2df900c93%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Ln2lT%2bjlNfLCRJLQzmBqmqvV9O0NLqA53g8wvVWqCAg%3d.

Member

chrisrpatterson commented Oct 28, 2015

It is in 2015 Update 1

From: Nicolas Penin [mailto:notifications@github.com]
Sent: Wednesday, October 28, 2015 8:05 AM
To: Microsoft/vso-agent-tasks vso-agent-tasks@noreply.github.com
Cc: Chris Patterson Chris.Patterson@microsoft.com
Subject: Re: [vso-agent-tasks] Persisting Powershell variables between steps (#375)

@chrisrpattersonhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fchrisrpatterson&data=01%7c01%7cChris.Patterson%40microsoft.com%7c3caf80c8a50348d2a1dd08d2df900c93%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=TJ4ZLAHjjMfWAmd6wYqbGbRpFuMn3Ebu2mrsO801BJw%3d Will it happen in TFS on premise ?


Reply to this email directly or view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fMicrosoft%2fvso-agent-tasks%2fissues%2f375%23issuecomment-151823523&data=01%7c01%7cChris.Patterson%40microsoft.com%7c3caf80c8a50348d2a1dd08d2df900c93%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Ln2lT%2bjlNfLCRJLQzmBqmqvV9O0NLqA53g8wvVWqCAg%3d.

@npenin

This comment has been minimized.

Show comment
Hide comment
@npenin

npenin Oct 28, 2015

2015 update 1 is available only in english, but our TFS has been installed in French :(

npenin commented Oct 28, 2015

2015 update 1 is available only in english, but our TFS has been installed in French :(

@chrisrpatterson

This comment has been minimized.

Show comment
Hide comment
@chrisrpatterson

chrisrpatterson Oct 28, 2015

Member

It is currently only in CTP and we don’t localize those. At RTM you will be able to get it in french.

From: Nicolas Penin [mailto:notifications@github.com]
Sent: Wednesday, October 28, 2015 9:54 AM
To: Microsoft/vso-agent-tasks vso-agent-tasks@noreply.github.com
Cc: Chris Patterson Chris.Patterson@microsoft.com
Subject: Re: [vso-agent-tasks] Persisting Powershell variables between steps (#375)

2015 update 1 is available only in english, but our TFS has been installed in French :(


Reply to this email directly or view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fMicrosoft%2fvso-agent-tasks%2fissues%2f375%23issuecomment-151851812&data=01%7c01%7cChris.Patterson%40microsoft.com%7cdd60d48ed7264b2f61d908d2df9f3e5f%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Ela%2b3rl3rAHf8ykTNnK5kSYkK1PdPQLDH8cB7KbmNmk%3d.

Member

chrisrpatterson commented Oct 28, 2015

It is currently only in CTP and we don’t localize those. At RTM you will be able to get it in french.

From: Nicolas Penin [mailto:notifications@github.com]
Sent: Wednesday, October 28, 2015 9:54 AM
To: Microsoft/vso-agent-tasks vso-agent-tasks@noreply.github.com
Cc: Chris Patterson Chris.Patterson@microsoft.com
Subject: Re: [vso-agent-tasks] Persisting Powershell variables between steps (#375)

2015 update 1 is available only in english, but our TFS has been installed in French :(


Reply to this email directly or view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fgithub.com%2fMicrosoft%2fvso-agent-tasks%2fissues%2f375%23issuecomment-151851812&data=01%7c01%7cChris.Patterson%40microsoft.com%7cdd60d48ed7264b2f61d908d2df9f3e5f%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Ela%2b3rl3rAHf8ykTNnK5kSYkK1PdPQLDH8cB7KbmNmk%3d.

@damienpontifex

This comment has been minimized.

Show comment
Hide comment
@damienpontifex

damienpontifex Nov 11, 2015

This will make the variable available later in the build and it should be in your environment. We have a behavior change coming in the next deployment (Early September) that will late bind variables references in inputs so you could set a variable in one task and then use it in an input via the $(testvar) syntax and the replacement will happen as expected.

@chrisrpatterson has this happened? And is there any documentation on using it?

damienpontifex commented Nov 11, 2015

This will make the variable available later in the build and it should be in your environment. We have a behavior change coming in the next deployment (Early September) that will late bind variables references in inputs so you could set a variable in one task and then use it in an input via the $(testvar) syntax and the replacement will happen as expected.

@chrisrpatterson has this happened? And is there any documentation on using it?

@chrisrpatterson

This comment has been minimized.

Show comment
Hide comment
@chrisrpatterson

chrisrpatterson Nov 11, 2015

Member

https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md you can see the list of commands you can use from scripts. you want task.setvariable

Member

chrisrpatterson commented Nov 11, 2015

https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md you can see the list of commands you can use from scripts. you want task.setvariable

@MiguelAlho

This comment has been minimized.

Show comment
Hide comment
@MiguelAlho

MiguelAlho Dec 8, 2015

Can a variable defined in a script (using the technique above) be used in the Repository tab's "Label Format" input?

I've defined a GitVersionTag variable, set in a script using:

Write-Host ##vso[task.setvariable variable=GitVersionTag;]$VersionString

Will the Label Format input late bind to this? My current run tags the repo with the "$(GitVersionTag)" text and not the value.

MiguelAlho commented Dec 8, 2015

Can a variable defined in a script (using the technique above) be used in the Repository tab's "Label Format" input?

I've defined a GitVersionTag variable, set in a script using:

Write-Host ##vso[task.setvariable variable=GitVersionTag;]$VersionString

Will the Label Format input late bind to this? My current run tags the repo with the "$(GitVersionTag)" text and not the value.

@MiguelAlho

This comment has been minimized.

Show comment
Hide comment
@MiguelAlho

MiguelAlho Dec 8, 2015

Figured it out! By setting build.updatebuildnumber with the logging command:

Write-Host ("##vso[build.updatebuildnumber]$versionToSet")

in the initial script (that's doing a few things other than just getting the version, such as updating assemblyInfo and nuspecs), I can then use $(build.buildNumber) in the Label Format input in the repository tab to tag the repo correctly!

MiguelAlho commented Dec 8, 2015

Figured it out! By setting build.updatebuildnumber with the logging command:

Write-Host ("##vso[build.updatebuildnumber]$versionToSet")

in the initial script (that's doing a few things other than just getting the version, such as updating assemblyInfo and nuspecs), I can then use $(build.buildNumber) in the Label Format input in the repository tab to tag the repo correctly!

@MylesArkell

This comment has been minimized.

Show comment
Hide comment
@MylesArkell

MylesArkell Feb 12, 2016

@chrisrpatterson
We have VSO 2015 on prem and just upgraded to update 1, however I have not been able to get this to work. Some people have commented that it was fixed in update 1. Has anyone been able to verify this?

Or am I doing something wrong?
##vso[task.setvariable variable=featurePackageName;]$targetNupkg
Write-Host "featurePackageName: "$Env:featurePackageName

the value shows up empty?

MylesArkell commented Feb 12, 2016

@chrisrpatterson
We have VSO 2015 on prem and just upgraded to update 1, however I have not been able to get this to work. Some people have commented that it was fixed in update 1. Has anyone been able to verify this?

Or am I doing something wrong?
##vso[task.setvariable variable=featurePackageName;]$targetNupkg
Write-Host "featurePackageName: "$Env:featurePackageName

the value shows up empty?

@ericsciple

This comment has been minimized.

Show comment
Hide comment
@ericsciple

ericsciple Feb 12, 2016

Member

@MylesArkell In your task/script, you would emit "##vso[task.setvariable variable=featurePackageName;]SomeValue" over STDOUT - if using ps1 script then this could be from unintercepted output or explicit Write-Host calls. If you immediately, check the value of $env:featurePackageName, then the value wouldn't be set. However if you check it in the next task that runs, then the value would be set. Does that help?

Member

ericsciple commented Feb 12, 2016

@MylesArkell In your task/script, you would emit "##vso[task.setvariable variable=featurePackageName;]SomeValue" over STDOUT - if using ps1 script then this could be from unintercepted output or explicit Write-Host calls. If you immediately, check the value of $env:featurePackageName, then the value wouldn't be set. However if you check it in the next task that runs, then the value would be set. Does that help?

@MylesArkell

This comment has been minimized.

Show comment
Hide comment
@MylesArkell

MylesArkell Feb 12, 2016

@ericsciple thank you very much for your quick reply Eric, I understand now and yes that works perfectly.
I have rewritten it as
Write-Host ("##vso[task.setvariable variable=featurePackageName;]$targetNupkg")

and this is indeed available in future tasks! Fantastic

MylesArkell commented Feb 12, 2016

@ericsciple thank you very much for your quick reply Eric, I understand now and yes that works perfectly.
I have rewritten it as
Write-Host ("##vso[task.setvariable variable=featurePackageName;]$targetNupkg")

and this is indeed available in future tasks! Fantastic

@tanyabr

This comment has been minimized.

Show comment
Hide comment
@tanyabr

tanyabr Jul 20, 2016

Hi,
I would like to do the same but have the new variable available in other environments. Is this possible? If so - how?

Thanks,
Tanya.

tanyabr commented Jul 20, 2016

Hi,
I would like to do the same but have the new variable available in other environments. Is this possible? If so - how?

Thanks,
Tanya.

@hcoona

This comment has been minimized.

Show comment
Hide comment
@hcoona

hcoona Feb 9, 2017

@chrisrpatterson This tricks only works for one-line varaible. I'd like to load copy file patterns from config file but it contains line-endings as seperator but be consumed in standard copy task in VSO. Is there a more powerful way for it?

hcoona commented Feb 9, 2017

@chrisrpatterson This tricks only works for one-line varaible. I'd like to load copy file patterns from config file but it contains line-endings as seperator but be consumed in standard copy task in VSO. Is there a more powerful way for it?

@ericsciple

This comment has been minimized.

Show comment
Hide comment
@ericsciple

ericsciple Feb 9, 2017

Member

you can replace carriage return and line feed with the URL encoded equivalents (%0D%0A)

Member

ericsciple commented Feb 9, 2017

you can replace carriage return and line feed with the URL encoded equivalents (%0D%0A)

@whihathac

This comment has been minimized.

Show comment
Hide comment
@whihathac

whihathac Jun 5, 2017

This will make the variable available later in the build and it should be in your environment.

Does this mean the variable will persist on other agent phases in the same Environment but different Agent Queues?

whihathac commented Jun 5, 2017

This will make the variable available later in the build and it should be in your environment.

Does this mean the variable will persist on other agent phases in the same Environment but different Agent Queues?

@iyerusad

This comment has been minimized.

Show comment
Hide comment
@iyerusad

iyerusad Aug 24, 2017

These variables do not appear to persist across phases, only tasks within the same phase.

Is there a method to have the variables persist across release phases?

CC: @whihathac @chrisrpatterson et al.

iyerusad commented Aug 24, 2017

These variables do not appear to persist across phases, only tasks within the same phase.

Is there a method to have the variables persist across release phases?

CC: @whihathac @chrisrpatterson et al.

@whihathac

This comment has been minimized.

Show comment
Hide comment
@whihathac

whihathac Aug 25, 2017

no clue, I am waiting for an answer too :)

whihathac commented Aug 25, 2017

no clue, I am waiting for an answer too :)

@iyerusad

This comment has been minimized.

Show comment
Hide comment
@iyerusad

iyerusad Sep 6, 2017

@whihathac variables across phases has issue here: #4743

iyerusad commented Sep 6, 2017

@whihathac variables across phases has issue here: #4743

@sufyannisar

This comment has been minimized.

Show comment
Hide comment
@sufyannisar

sufyannisar Nov 28, 2017

I am using PowerShellOnTargetMachines Using File
I want to change variable value but
Whenever I use Write-Host in Powershell script I get error saying

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

sufyannisar commented Nov 28, 2017

I am using PowerShellOnTargetMachines Using File
I want to change variable value but
Whenever I use Write-Host in Powershell script I get error saying

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

@ederbond

This comment has been minimized.

Show comment
Hide comment
@ederbond

ederbond Jan 9, 2018

Me too @sufyannisar

I am using PowerShellOnTargetMachines Using File
I want to change variable value but
Whenever I use Write-Host in Powershell script I get error saying

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

ederbond commented Jan 9, 2018

Me too @sufyannisar

I am using PowerShellOnTargetMachines Using File
I want to change variable value but
Whenever I use Write-Host in Powershell script I get error saying

A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

@weznagwama

This comment has been minimized.

Show comment
Hide comment
@weznagwama

weznagwama Jan 10, 2018

I'd like to echo Tanya's question: are you able to set/update a variable inside a release, for use in another release?

Or, are you able to update the value of a variable inside of a shared variable group? This could perhaps be in the realm of the API, which I haven't yet looked at...

edit: doesn't look like it.

What i need to do is conditionally deploy a release, as long as another release has been deployed. This is because there's an explicit dependency between the 2 release. In octopus deploy, we have achieved this using variables (true/false or 1/0 if a release/process step is good to go).

weznagwama commented Jan 10, 2018

I'd like to echo Tanya's question: are you able to set/update a variable inside a release, for use in another release?

Or, are you able to update the value of a variable inside of a shared variable group? This could perhaps be in the realm of the API, which I haven't yet looked at...

edit: doesn't look like it.

What i need to do is conditionally deploy a release, as long as another release has been deployed. This is because there's an explicit dependency between the 2 release. In octopus deploy, we have achieved this using variables (true/false or 1/0 if a release/process step is good to go).

@cserkaran

This comment has been minimized.

Show comment
Hide comment
@cserkaran

cserkaran Jan 18, 2018

Is it possible to overwrite System variables e.g. $Env:SYSTEM_DEFAULTWORKINGDIRECTORY in the powershell script using Write-Host

cserkaran commented Jan 18, 2018

Is it possible to overwrite System variables e.g. $Env:SYSTEM_DEFAULTWORKINGDIRECTORY in the powershell script using Write-Host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment