Can't access environment variables in Windows Powershell

Hi!

I’m trying out the newly added Windows support and I am unable to access secret environment variables (set via Project => Build Settings => Environment Variables) when using Powershell.

See below for a minimal repro. In the examples I’ve already set an environment variable TEST_VAR with value test in the web UI.

version: 2.1

orbs:
  win: circleci/windows@1.0.0

jobs:
  build:
    executor:
      name: win/vs2019
      shell: powershell.exe
    environment:
      FOO: "bar"    
    steps:
      - checkout
      - run: Write-Host "Hello $TEST_VAR" # Prints "Hello "
      - run: Write-Host "Hello $FOO" # Prints "Hello "
      - run:
          name: Write-Host
          command: |
            $BAS = "bas"
            Write-Host "Hello $BAS" # Prints "Hello bas"

Note however that this does work with the bash.exe shell:

version: 2.1

orbs:
  win: circleci/windows@1.0.0

jobs:
  build:
    executor:
      name: win/vs2019
      shell: bash.exe
    environment:
      FOO: "bar"    
    steps:
      - checkout
      - run: Write-Host "Hello $FAKE_GCLOUD_SERVICE_KEY" # Prints "Hello test"
      - run: Write-Host "Hello $FOO" # Prints "Hello bar"

At first I thought that this might have to do with CircleCI not printing secrets, but as seen in the second example, that’s not the case. Furthermore the commands that consume the variables all fail.

Thanks for reporting this issue. We are looking into it and will update here if we need more details from you, or if we have an update.

1 Like

Hi eysi09,

The issue here is with the powershell script that you have provided. I have confirmed that environment variables are working as intended in PowerShell.

Write-Host "Hello $FOO"

This will print the value of the variable “FOO”.

$BAS = "bas"

This will set the variable “BAS” to the value bas.

To read an environment variable in PowerShell you need to prefix $env:, like so:

Write-Host "Hello $env:FOO"

Let me know if you have any more questions,

Marc

Thank you for the reply! I haven’t verified on my end but this looks right so I’ll go ahead and marc it as a solution (that’s not a typo, just a bad pun).

Eythor

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.