Zohaib Khan
Zohaib Khan

Reputation: 5

How to store variable in Azure YAML pipeline using a Powershell expression

I have two stages in the pipeline: build (Microsoft Hosted) and Invoke (agentless). After building the code, I added a PowerShell task to retrieve an EXE version

      - task: PowerShell@2
        displayName: GetVar
        inputs:
          targetType: 'inline'
          script: |
           '$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion'
            Write-Host 'Version: $fileVersion'

However, my Write-Host command prints a blank line. I want to add this $fileVersion variable in the Azure Function query parameters.

I replaced the script section with the following code: Docs

Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true](Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion"
Write-Host "Version: $FileVersion"

However, it doesn't evaluate the expression and instead writes it as it is, as shown below:

(Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion

I tried referring to the variable as follows, but it didn't work:

powershell: Write-Host "this is $env:Version"

I want to retrieve the FileVersion and use it in another stage for an Azure Function as query param.

For now I am manually injecting Version in the pipeline code.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
  branches:
    include:
      - develop
      - release
      - main

variables:
  solution: "**/*.sln"
  buildPlatform: "Any CPU"
  buildConfiguration: "Release"


stages:
 - stage: BuildandCopy
   pool:
     vmImage: windows-latest
   jobs:
   - job: Build

     steps:
      - task: NuGetToolInstaller@1
        displayName: Install Nuget tools
        inputs:
          versionSpec: "6"

      - task: NuGetCommand@2
        displayName: Restore Solution Packages
        inputs:
          restoreSolution: "$(solution)"

      - task: VSBuild@1
        displayName: Building Solution
        inputs:
          solution: "$(solution)"
          vsVersion: latest
          msbuildArgs: '/p:SkipInvalidConfigurations=true /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:publishUrl="$(build.artifactstagingdirectory)\\" /p:DeployDefaultTarget=WebPublish'
          platform: "$(buildPlatform)"
          configuration: "$(buildConfiguration)"

      - powershell: Write-Host "##vso[task.setvariable variable=FileVersion;isOutput=true]2.0.0.0"
        name: getVar

      - powershell: Write-Host  'this is $(getVar.FileVersion)'
      - task: PublishBuildArtifacts@1
        displayName: "Publish APIs"


 - stage: InvokeAzureFunction
   jobs:
   - job: Invoke
     variables:
       exeVersion: $[stageDependencies.BuildandCopy.build.outputs['getVar.FileVersion']]
     pool: server
     steps:
      - task: AzureFunction@1
        inputs:
          function: 'URL'
          key: 'mykey=='
          method: 'POST'
          queryParameters: 'Version=$(exeVersion)'
          waitForCompletion: 'false'

Upvotes: 0

Views: 621

Answers (1)

Robert
Robert

Reputation: 312

In the first PowerShell task, you seem to have the '$fileVersion = (Get-Item TargetEngine.Aspen.Services.Internal.JobScheduler\bin\Release\net6.0\TargetEngine.Aspen.Services.Internal.JobScheduler.exe).VersionInfo.FileVersion' encased in single quotes. By doing that, you are just declaring a string there and not assigning the .exe version to a variable. The reason the $fileVersion is empty is that it is, as it was never assigned anything and has only been created during the Write-Host command. Try removing the single quotes and see if that works.

Update:

You also seem to have single quotes around the Write-Host output.

Here's an updated test that I ran and it works fine. I just changed the .exe path to something that I had on my agent.

  displayName: GetVar
  inputs:
    targetType: 'inline'
    script: |
      cd "c:\Program Files\Microsoft SQL Server\160\DAC\bin"
      $fileVersion = (Get-Item SqlPackage.exe).VersionInfo.FileVersion
      Write-Host "Version: $fileVersion"

Logs

Starting: GetVar
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.220.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\fd8338df-b2d0-437a-90d5-aaad1b24b079.ps1'"
Version: 16.1.8089.0
Finishing: GetVar

Upvotes: 0

Related Questions