Pass a Terraform Output to an Azure DevOps Varibale

i’m trying to create an Azure DEvOps pipeline that deploy a VM using terraform, and then connect to that VM using WinRm to execute script but i can’t find a way to pass the Public IP captured from output.tf to the pipeline to be used on the connection. Hope somebody can give some advice to solve this problem.

See below my .yml file

    trigger:
  none

pool:
  vmImage: 'windows-latest'

variables:
  bkrg: 'CloudOPs_OPS_RG'
  bkstorage: 'xxxxxxxxxx'
  bkcontainer: 'terraformmtp'
  bkkey: 'terraform.tfstate'
  adminUsername: 'xxxxxxxxxxxx'
  adminPassword: 'xxxxxxxxxxxxx' 

parameters:
  - name: bacpacSasToken
    displayName: 'Bacpac SAS Token'
    type: string

stages:
- stage: 'tfvalidate'
  jobs:
    - job: 'validate'
      continueOnError: false
      steps:
        - task: TerraformInstaller@0
          displayName: 'Install Terraform'
          inputs:
            terraformVersion: 'latest'

        - task: TerraformTaskV3@3
          displayName: 'Terraform init'
          inputs:
            provider: 'azurerm'
            command: 'init'
            backendServiceArm: 'xxxxxxxxxxxxxxxx'
            backendAzureRmResourceGroupName: '$(bkrg)'
            backendAzureRmStorageAccountName: '$(bkstorage)'
            backendAzureRmContainerName: '$(bkcontainer)'
            backendAzureRmKey: '$(bkkey)'

        - task: TerraformTaskV3@3
          displayName: 'Terraform validate'
          inputs:
            provider: 'azurerm'
            command: 'validate'

- stage: 'tfdeploy'
  condition: succeeded('tfvalidate')
  dependsOn: 'tfvalidate'
  jobs:
    - job: 'apply'
      steps:
        - task: TerraformInstaller@0
          displayName: 'Install Terraform'
          inputs:
            terraformVersion: 'latest'

        - task: TerraformTaskV3@3
          displayName: 'Terraform init'
          inputs:
            provider: 'azurerm'
            command: 'init'
            backendServiceArm: 'xxxxxxxxxxxxxxxx'
            backendAzureRmResourceGroupName: '$(bkrg)'
            backendAzureRmStorageAccountName: '$(bkstorage)'
            backendAzureRmContainerName: '$(bkcontainer)'
            backendAzureRmKey: '$(bkkey)'

        - task: TerraformTaskV3@3
          displayName: 'Terraform plan'
          inputs:
            provider: 'azurerm'
            command: 'plan'
            environmentServiceNameAzureRM: 'xxxxxxxxxxxxxxxx'

        - task: TerraformTaskV3@3
          displayName: 'Terraform apply'
          inputs:
            provider: 'azurerm'
            command: 'apply'
            environmentServiceNameAzureRM: 'xxxxxxxxxxxxxxxx'

        - task: PowerShell@2
          displayName: 'Capture public IP and push to pipeline variable'
          inputs:
            targetType: 'inline'
            script: |
              $tfOutput = terraform output -json | ConvertFrom-Json
              $publicIp = $tfOutput.public_ip.value
              Write-Host "##vso[task.setvariable variable=publicIp;isOutput=true]$publicIp"
              Write-Host "Captured Public IP: $publicIp"

- stage: 'bacpacoperations'
  condition: succeeded('tfdeploy')
  dependsOn: 'tfdeploy'
  jobs:
    - job: 'downloadbacpac'
      variables:
        public_ip: $[ stageDependencies.tfdeploy.apply.outputs['publicIp'] ]
      steps:
        - task: AzureCLI@2
          displayName: 'Azure CLI'
          inputs:
            azureSubscription: 'xxxxxxxxxxxxxxxx'
            scriptType: 'ps' 
            scriptLocation: 'inlineScript'
            inlineScript: |
              $publicip = "$(public_ip)"
              $username = "$(adminUsername)"
              $password = "$(adminPassword)"
              $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
              $credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
              $session = New-PSSession -ComputerName $publicip -Credential $credential -UseSSL -Port 5986
              Invoke-Command -Session $session -ScriptBlock { 
                cd $(System.DefaultWorkingDirectory)
                ./downloadbacpac.ps1 -bacpacSasToken "$(bacpacSasToken)" -localPath "f:\"
              }
              Remove-PSSession -Session $session

this is the error i’m getting

New-PSSession : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an 
argument that is not null or empty, and then try the command again.
At D:a_tempazureclitaskscript1726051757039_inlinescript.ps1:6 char:40
+ $session = New-PSSession -ComputerName $publicip -Credential $credent ...
+                                        ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-PSSession], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewPSSessionCommand
 

##[error]Script failed with exit code: 1

To use variable in next stage, please add task name in the powershell task in tfdeploy stage.

        - task: PowerShell@2
          displayName: 'Capture public IP and push to pipeline variable'
          name: getip             # <-- add task name
          inputs:
            targetType: 'inline'
            script: |
              $tfOutput = terraform output -json | ConvertFrom-Json
              $publicIp = $tfOutput.public_ip.value
              Write-Host "##vso[task.setvariable variable=publicIp;isOutput=true]$publicIp"
              Write-Host "Captured Public IP: $publicIp"   # < -- make sure ip value output is correct

And in next stage, include the task name in variable expression:

      variables:
        public_ip: $[ stageDependencies.tfdeploy.apply.outputs['getip.publicIp'] ]

Please refer to the doc Use outputs in a different stage for the details.

In powershell task, please make sure the agent ip $publicIp is correctly output, as the value will be set as the output variable value to next stage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật