0

My azure pipelines yaml runs a powershell script which is stored inside the repo.

That powershell script expects 3 variables : the working directory, the Oauth access token and the source branch name (which triggered the pipeline).

But it seems , whenever I try to pass on parameters, the powershell script does not recognize them and I get an error

The term 'env:SYSTEM_ACCESSTOKEN' is not recognized as the 
name of a cmdlet, function, script file, or operable program

The term 'env:BUILD_SOURCEBRANCHNAME' is not recognized as the 
name of a cmdlet, function, script file, or operable program

My yaml looks like this:

name: $(Build.DefinitionName)_$(Build.SourceBranchName)_$(Build.BuildId)

trigger:
  branches:
    include:
      - '*'

variables:
  system_accesstoken: $(System.AccessToken)

jobs:
  - job: NoteBookMergeAndOnPremSync
    displayName: Merge Notebooks to Notebooks branch and sync to on prem git
    pool:
      name: Poolname
    steps:
    - task: PowerShell@2
      displayName: 'Merge to Notebooks branch in Azure and Sync to On Prem'
      inputs:
        targetType: filePath
        filePath: ./deploy/MergeAndSync.ps1
        arguments: '-workingdir $(System.DefaultWorkingDirectory)\ -featurebranch $(env:BUILD_SOURCEBRANCHNAME) -accesstoken $(env:SYSTEM_ACCESSTOKEN)'
    

I was able to run the powershell script successfully as an "in line powershell script" when using a "release definition" using the GUI, but I would like all that to be in an azure pipeline (yaml)in yaml but unfortunately, I just can't find a way to pass on these env variables.

How do I pass on the BUILD_SOURCEBRANCHNAME and env:SYSTEM_ACCESSTOKEN to the powershell script from the azure pipeline yaml?

Also, I would like to avoid an "inline powershell script" and rather have the logic saved in my repo.

Saugat Mukherjee
  • 778
  • 8
  • 32

1 Answers1

3

It looks like you're mixing Azure macro syntax ($(name)) with PowerShell variable-reference syntax ($env:name, for environment variables).

That said, since you're invoking a script file with arguments - which presumably means that the -File parameter of the PowerShell CLI is used - you cannot reference environment variables in the arguments, because PowerShell then interprets something like $env:BUILD_SOURCEBRANCHNAME verbatim (as a literal string) rather than as an environment-variable reference (the latter would only work inside the script or in CLI calls using -Command).

Therefore, I presume the solution is to use only Azure macro syntax to pass the value of the variables of interest as arguments:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(system_accesstoken)

Update: As you state, you didn't need any variables definition: referencing $(System.AccessToken) directly works too:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(System.AccessToken)
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thanks , that was right. Also,I feel so stupid. The answer was right on top of my build defintion, where I defined the name, in the very first line (just a long day). But anyways, thanks for the elaboration. Without it, I would not have known more about the nuances of -File and -Command parameters of the PowerShell CLI. Thanks a lot ! – Saugat Mukherjee Dec 10 '21 at 20:40
  • I just did this : arguments: '-workingdir $(System.DefaultWorkingDirectory)\ -featurebranch $(Build.SourceBranchName) -accesstoken $(System.AccessToken)' . And yeah, the variables section is redundant, so I removed it – Saugat Mukherjee Dec 10 '21 at 20:43
  • Glad to hear it helped, @SaugatMukherjee; my pleasure. I've updated the answer to show your no-`variables:` solution. – mklement0 Dec 10 '21 at 21:42
  • 1
    Thanks for the edit, @VinceBowdren - didn't know about this syntax (have since found an explanation [here](https://stackoverflow.com/a/21699210/45375)). – mklement0 Dec 11 '21 at 02:01