0

I have below code in yaml file:

trigger:
   branches:
        include: 
        - maventest
pool:

  vmImage: ubuntu-latest

steps:

- task: PowerShell@2
  inputs:
    filePath: './1.ps1'

Below is 1.ps1 file

$user1=@($(user))

Write-Host $(user)

write-host $user1[0]

As I can pass the value of variable inside while run pipeline but user is not getting from variable. enter image description here

I had also used the $(env:user) but it didn't get value from variable. enter image description here

1 Answer 1

2

From your YAML sample, the cause of the issue is that the pipeline variable is not able to directly expand in PowerShell file.

To solve this issue, you can pass the pipeline variable to ps file via argument of PowerShell task.

Here is the example:

Ps file sample:

param($user)
$user1=@($user)

Write-Host $user

write-host $user1[0]

Pipeline sample:

steps:

- task: PowerShell@2
  inputs:
    filePath: './1.ps1'
    arguments: '-user $(user)'

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @kevin for taking time to answer the question , you have saved my lot of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.