简体   繁体   中英

Passing variable from one powershell task to other powershell task with in same step in YAML

I am using Azure DevOps pipeline to execute test on using TAEF from powershell, and want to share the test output from mail. Pipeline outputs following error:

Invoke-RestMethod: {"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Unexpected character encountered while parsing value: }. Path 'email_body', line 5, position 0.'."}}

Following is the code in my Pipeline.

- powershell: |
          echo "Running TAEF"
          $output = & 'C:\Program Files (x86)\Windows Kits\10\Testing\Runtimes\TAEF\TE.exe' x.dll
          $jsonOutput = $output | ConvertTo-Json

- task: PowerShell@2
        displayName: Send Email
        inputs:
          targetType: inline
          script: |
            $url = "xxxx"

            $headers = @{
                "Content-Type" = "application/json"
            }

            $emailMessage = @"
            {
                "to": "abc@abc.com",
                "subject": "Test",
                "email_body": $jsonOutput
            }
            "@

            Invoke-RestMethod -Method Post -Uri $url -Headers $headers -Body $emailMessage

Couple of questions: Can we use defined variable from powershell into another powershell task? If not how to achieve it?

Yes, you can set variables in you powershell script and use them in your subsequent tasks and throughout your jobs. According to the documentation

When you add a variable with task.setvariable , the following tasks can use the variable using macro syntax $(myVar) . The variable will only be available to tasks in the same job by default.

Example

- powershell: |
    Write-Host "##vso[task.setvariable variable=myVar;]foo"
- task: PowerShell@2
  displayName: Next powershell task
  inputs:
    targetType: 'inline'
    script: 'Write-Host "You can use macro syntax for variables: $(myVar)"'

Output

You can use macro syntax for variables: foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM