0
votes

We are using powershell to do some deployment tasks in the target environment(e.g. a VM). However, it seems the script does not have access to the system environment variables, as well as other programs run by the script. So far, it only has access to the variables defined in the pipeline.

Also, inside the .NET program run by that script, we have:

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Machine);

but it still does not have access to the desired environment variable.

Is there a way we can allow the pipeline script to access the system variable? Or, do we have to define a stage-scoped pipeline variable and make it consistent with the targeting machine's system variable?

Thanks.

1
do you try to run Get-ChildItem Env: to check out your available env:variables?Mar Tin
Yes, this is what I have for the Env: Env:. Obviously, most of them are for pipeline purpose, missing the system variable though.Smokovsky
Do you try to define your system variable like $format = "yyyyMMdd_HHmmss"; $date = Get-Date; Write-Host "##vso[task.setvariable variable=Timestamp]$($date.ToString($format))" inside a pipeline task? (example definition, no relation to your question)Mar Tin
@Smokovsky Not get your response for several days, would you please share your latest information about this issue? If you have any concern, feel free to share it here.Hugh Lin
@HughLin-MSFT I was pretty sure that the script was run on the target machine as the program called by that script was interacting with system API to setup stuff like windows service. I was just really interested in how all system variables are hidden from it as well as all its child process.Smokovsky

1 Answers

0
votes

If you refer to get the system environment variables of the target machine, you just need to run the pipeline with the self-hosted agent installed on the target machine. Then use $ env: VARIABLE_NAME in the powershell script to get the system environment variables of the target machine.

The system environment variables of the machine obtained by powershell script in Azure Devops are determined by the agent that runs the pipeline. The agent on which machine is used to run the pipeline can access the system environment variables of that machine.

In addition, you can try the method mentioned by Mar Tin in the comment to define system variables as pipeline variables.