Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to Resolve PowerShell Invoke-Expression Error When Using Environment Variables in Bitbucket Pipe

Yogesh Kumar August 20, 2024

 

 

Hello everyone,

I'm encountering an issue with a Bitbucket pipeline running on a self-hosted Windows runner. I’m trying to use environment variables to execute nuget.exe via PowerShell, but I’m getting an error related to Invoke-Expression.

 

Invoke-Expression : The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object. At C:\atlassian-bitbucket-pipelines-runner\temp\...\tmp\powershellScript.ps1:11 char:50 + '& $env:NUGET_PATH restore $env:SOLUTION_PATH' | Invoke-Expression + ~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-Expression], RuntimeException + FullyQualifiedErrorId : BadExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand

Below is my YAML script:

 

pipelines:

variables:

 NUGET_PATH: 'C:\Nuget\nuget.exe'

MSBUILD_PATH: 'some path'

MAIN_PROJECT_PATH:'some path'

SOLUTION_PATH : 'some path'

branches:

branch1:

- step:

  name: Building the branch1

runs-on:

- self.hosted

- windows

script:

- "& $env:NUGET_PATH restore $env:SOLUTION_PATH"

 

 

 

I’ve tried different variations, including using backticks and escaping, but I’m still facing the same error. Has anyone encountered this issue or know how to correctly use environment variables in this context? (My syntax in yaml file is coorect when i paste in the question bar so it is not properly formatted)

 

 

Any help would be greatly appreciated!

 

1 answer

1 accepted

1 vote
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 22, 2024

Hello @Yogesh Kumar ,

and welcome to the Community!

Checking the YAML file you shared, I can think of two issues that are causing the error :

  1. Variables definitions: I see you defined the variables directly in the YML and that syntax is not actually valid. To define variables to be used in your pipelines, you'll need to create the variables at the workspace, repository or deployment level.
  2. The syntax you have used for the command itself is slightly wrong. The following syntax should work:
    script:

    - |

    & "$env:MyCommand" -v

 
Could you try making those changes and let us know how it goes?

Thank you, @Yogesh Kumar !

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events