0

I'm trying to asign the result of a powershell command to MY_VAR environment variable. I have tried several ways but I cannot get the variable to take the value of the operation. If I assign the variable as follows what I get as a value is the command literally.

ARG MY_ARG="VALUE"

ENV MY_VAR=[Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes($MY_ARG))

ENV MY_VAR2=$([Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes($MY_ARG)))

When I check the values into the container I get this:

Get-Childitem -Path Env:MY_VAR*

Name                           Value                                                                                                                                              
----                           -----                                                                                                                                              
MY_VAR                         [Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes(VALUE))  
MY_VAR2                        $([Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes(VALUE)))

The base of my containers are Windows Server Core and my shell is powershell.

Alpha75
  • 2,140
  • 1
  • 26
  • 49

1 Answers1

0

See this SO post , it contains several workarounds to achieve that you want.

ENV treat value as a simple text string.

UPDATE:

According to answers and comments here:

1) RUN $profile that would show you location of environment profile. Get more familiar with this file.

2) Try RUN $env.MY_VAR = [Convert]::ToBase64String([system.Text.Encoding]::UTF8.GetBytes($MY_ARG)) >> $profile or another way to append the command to file. I am not familiar enough with powershell, thus be aware that you maybe would to fix slightly the command. (Comment with the right command and I will fix it for next viewers.)

3) Try to read MY_VAR in the container. If all is right, then Hoooray!, else check in the $profile that you actually get the right string of setting the variable.

Nick Vee
  • 621
  • 2
  • 7
  • 17
  • Thanks!, I've tried some of them but I couldn't translate to powershell successfully. My containers are windows based. – Alpha75 Mar 30 '20 at 10:41
  • I need the variable available during the build of the image. I'm afraid that method is only for the container. – Alpha75 Mar 31 '20 at 15:14
  • I do not have enough information about powershell but the linux analogue (/etc/bash.bashrc) works fine for building usage. The variable is initialized from the file on every build step (in every intermediate container), thus you can use a env var during building as the env var defined using ENV. – Nick Vee Mar 31 '20 at 16:51