How can I get a task to set a variable that can be used in a configuration?












1














In VS Code I have defined a task:



"tasks": [
{
"label": "getcredentials",
"type": "shell",
"command": ".\TestScripts\GetCredentials.ps1"
}
]


GetCredentials.ps1 creates a credential and assigns it to $Global:credential.



In launch.json I want to use $Global:credential as an arg to the actual script I am debugging:



    "configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "Exchange Parameter Set, No Inactive",
"preLaunchTask": "getcredentials",
"script": "${file}",
"args": [ "-Credential $Global:credential"],
"cwd": "${file}"
}
]


However, the script doesn't get the value of $Global:credential (it instead prompts for credentials). I thought that this should be possible since this https://code.visualstudio.com/docs/editor/tasks-appendix says that the parent process environment is used if no environment is specified.



Any idea what I'm missing, or is this impossible?










share|improve this question




















  • 1




    I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
    – Ansgar Wiechers
    Jul 13 at 10:44
















1














In VS Code I have defined a task:



"tasks": [
{
"label": "getcredentials",
"type": "shell",
"command": ".\TestScripts\GetCredentials.ps1"
}
]


GetCredentials.ps1 creates a credential and assigns it to $Global:credential.



In launch.json I want to use $Global:credential as an arg to the actual script I am debugging:



    "configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "Exchange Parameter Set, No Inactive",
"preLaunchTask": "getcredentials",
"script": "${file}",
"args": [ "-Credential $Global:credential"],
"cwd": "${file}"
}
]


However, the script doesn't get the value of $Global:credential (it instead prompts for credentials). I thought that this should be possible since this https://code.visualstudio.com/docs/editor/tasks-appendix says that the parent process environment is used if no environment is specified.



Any idea what I'm missing, or is this impossible?










share|improve this question




















  • 1




    I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
    – Ansgar Wiechers
    Jul 13 at 10:44














1












1








1







In VS Code I have defined a task:



"tasks": [
{
"label": "getcredentials",
"type": "shell",
"command": ".\TestScripts\GetCredentials.ps1"
}
]


GetCredentials.ps1 creates a credential and assigns it to $Global:credential.



In launch.json I want to use $Global:credential as an arg to the actual script I am debugging:



    "configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "Exchange Parameter Set, No Inactive",
"preLaunchTask": "getcredentials",
"script": "${file}",
"args": [ "-Credential $Global:credential"],
"cwd": "${file}"
}
]


However, the script doesn't get the value of $Global:credential (it instead prompts for credentials). I thought that this should be possible since this https://code.visualstudio.com/docs/editor/tasks-appendix says that the parent process environment is used if no environment is specified.



Any idea what I'm missing, or is this impossible?










share|improve this question















In VS Code I have defined a task:



"tasks": [
{
"label": "getcredentials",
"type": "shell",
"command": ".\TestScripts\GetCredentials.ps1"
}
]


GetCredentials.ps1 creates a credential and assigns it to $Global:credential.



In launch.json I want to use $Global:credential as an arg to the actual script I am debugging:



    "configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "Exchange Parameter Set, No Inactive",
"preLaunchTask": "getcredentials",
"script": "${file}",
"args": [ "-Credential $Global:credential"],
"cwd": "${file}"
}
]


However, the script doesn't get the value of $Global:credential (it instead prompts for credentials). I thought that this should be possible since this https://code.visualstudio.com/docs/editor/tasks-appendix says that the parent process environment is used if no environment is specified.



Any idea what I'm missing, or is this impossible?







powershell visual-studio-code vscode-tasks






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 at 13:15









Gama11

10.8k21946




10.8k21946










asked Jul 13 at 3:12









yltlatl

85




85








  • 1




    I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
    – Ansgar Wiechers
    Jul 13 at 10:44














  • 1




    I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
    – Ansgar Wiechers
    Jul 13 at 10:44








1




1




I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
– Ansgar Wiechers
Jul 13 at 10:44




I have very little experience with Visual Studio. Do the individual tasks get launched as individual processes? In that case passing variables between tasks is not going to work, b/c you can't pass (PowerShell) variables across process boundaries.
– Ansgar Wiechers
Jul 13 at 10:44












1 Answer
1






active

oldest

votes


















0














https://code.visualstudio.com/docs/editor/tasks-appendix states with respect to the optional env key in a task definition:



   /**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/


This relates to environment variables, not PowerShell sessions.



Furthermore, the parent process environment being referred to is Visual Studio's own block of environment variables.



In other words:




  • Since the PS task session has exited by the time the PS debugging session starts, you cannot pass variables between the two sessions.


  • Even attempting to set environment variables in your PS task session won't work, because both sessions are run with VS Code as the parent process and cannot see each other's environment modifications.





Your best bet is to use (temporary) files to communicate values from the task session to the debugging session.



See this answer for how to save to and restore credentials from a file.
Caveat: Works on Windows only.



Choose a file location (in the simplest case with a fixed name, e.g. $HOME/.test-creds.clixml) that the task session writes to, and the debugging session then reads from, along the lines of:



"args": [ "-Credential (Import-CliXml $HOME/.test-creds.clixml)" ],





share|improve this answer



















  • 1




    This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
    – yltlatl
    Jul 17 at 1:11










  • @yltlatl: My pleasure; glad to hear it was helpful.
    – mklement0
    Jul 17 at 1:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51317136%2fhow-can-i-get-a-task-to-set-a-variable-that-can-be-used-in-a-configuration%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














https://code.visualstudio.com/docs/editor/tasks-appendix states with respect to the optional env key in a task definition:



   /**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/


This relates to environment variables, not PowerShell sessions.



Furthermore, the parent process environment being referred to is Visual Studio's own block of environment variables.



In other words:




  • Since the PS task session has exited by the time the PS debugging session starts, you cannot pass variables between the two sessions.


  • Even attempting to set environment variables in your PS task session won't work, because both sessions are run with VS Code as the parent process and cannot see each other's environment modifications.





Your best bet is to use (temporary) files to communicate values from the task session to the debugging session.



See this answer for how to save to and restore credentials from a file.
Caveat: Works on Windows only.



Choose a file location (in the simplest case with a fixed name, e.g. $HOME/.test-creds.clixml) that the task session writes to, and the debugging session then reads from, along the lines of:



"args": [ "-Credential (Import-CliXml $HOME/.test-creds.clixml)" ],





share|improve this answer



















  • 1




    This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
    – yltlatl
    Jul 17 at 1:11










  • @yltlatl: My pleasure; glad to hear it was helpful.
    – mklement0
    Jul 17 at 1:13
















0














https://code.visualstudio.com/docs/editor/tasks-appendix states with respect to the optional env key in a task definition:



   /**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/


This relates to environment variables, not PowerShell sessions.



Furthermore, the parent process environment being referred to is Visual Studio's own block of environment variables.



In other words:




  • Since the PS task session has exited by the time the PS debugging session starts, you cannot pass variables between the two sessions.


  • Even attempting to set environment variables in your PS task session won't work, because both sessions are run with VS Code as the parent process and cannot see each other's environment modifications.





Your best bet is to use (temporary) files to communicate values from the task session to the debugging session.



See this answer for how to save to and restore credentials from a file.
Caveat: Works on Windows only.



Choose a file location (in the simplest case with a fixed name, e.g. $HOME/.test-creds.clixml) that the task session writes to, and the debugging session then reads from, along the lines of:



"args": [ "-Credential (Import-CliXml $HOME/.test-creds.clixml)" ],





share|improve this answer



















  • 1




    This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
    – yltlatl
    Jul 17 at 1:11










  • @yltlatl: My pleasure; glad to hear it was helpful.
    – mklement0
    Jul 17 at 1:13














0












0








0






https://code.visualstudio.com/docs/editor/tasks-appendix states with respect to the optional env key in a task definition:



   /**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/


This relates to environment variables, not PowerShell sessions.



Furthermore, the parent process environment being referred to is Visual Studio's own block of environment variables.



In other words:




  • Since the PS task session has exited by the time the PS debugging session starts, you cannot pass variables between the two sessions.


  • Even attempting to set environment variables in your PS task session won't work, because both sessions are run with VS Code as the parent process and cannot see each other's environment modifications.





Your best bet is to use (temporary) files to communicate values from the task session to the debugging session.



See this answer for how to save to and restore credentials from a file.
Caveat: Works on Windows only.



Choose a file location (in the simplest case with a fixed name, e.g. $HOME/.test-creds.clixml) that the task session writes to, and the debugging session then reads from, along the lines of:



"args": [ "-Credential (Import-CliXml $HOME/.test-creds.clixml)" ],





share|improve this answer














https://code.visualstudio.com/docs/editor/tasks-appendix states with respect to the optional env key in a task definition:



   /**
* The environment of the executed program or shell. If omitted
* the parent process' environment is used.
*/


This relates to environment variables, not PowerShell sessions.



Furthermore, the parent process environment being referred to is Visual Studio's own block of environment variables.



In other words:




  • Since the PS task session has exited by the time the PS debugging session starts, you cannot pass variables between the two sessions.


  • Even attempting to set environment variables in your PS task session won't work, because both sessions are run with VS Code as the parent process and cannot see each other's environment modifications.





Your best bet is to use (temporary) files to communicate values from the task session to the debugging session.



See this answer for how to save to and restore credentials from a file.
Caveat: Works on Windows only.



Choose a file location (in the simplest case with a fixed name, e.g. $HOME/.test-creds.clixml) that the task session writes to, and the debugging session then reads from, along the lines of:



"args": [ "-Credential (Import-CliXml $HOME/.test-creds.clixml)" ],






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 13 at 21:41

























answered Jul 13 at 21:34









mklement0

126k20239267




126k20239267








  • 1




    This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
    – yltlatl
    Jul 17 at 1:11










  • @yltlatl: My pleasure; glad to hear it was helpful.
    – mklement0
    Jul 17 at 1:13














  • 1




    This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
    – yltlatl
    Jul 17 at 1:11










  • @yltlatl: My pleasure; glad to hear it was helpful.
    – mklement0
    Jul 17 at 1:13








1




1




This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
– yltlatl
Jul 17 at 1:11




This worked like a charm, thanks mklement0! Didn't realize you could run code in the args. This is much simpler than what I was trying to do.
– yltlatl
Jul 17 at 1:11












@yltlatl: My pleasure; glad to hear it was helpful.
– mklement0
Jul 17 at 1:13




@yltlatl: My pleasure; glad to hear it was helpful.
– mklement0
Jul 17 at 1:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51317136%2fhow-can-i-get-a-task-to-set-a-variable-that-can-be-used-in-a-configuration%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas