skip to Main Content

I have a myJson.json that look like this:

{
  "FirewallGroupsToEnable": [
    "Remote Event Log Management",
    "Windows Remote Management",
    "Performance Logs and Alerts",
    "File and Printer Sharing",
    "Windows Management Instrumentation (WMI)"
  ],
  "MemoryStartupBytes": "3GB"
}

I’d like to serialize it as a string and then set it as a variable to be used by other tasks. If there is a better way to use this file inside a pipeline please let me know.

I’m serializing it and setting it like this:

          - task: PowerShell@2
            inputs:
              targetType: 'inline'
              script: |

                  $Configs= Get-Content -Path $(Build.SourcesDirectory)sourcesmyJson.json -Raw | ConvertFrom-Json
                  
                  Write-Host "##vso[task.setvariable variable=Configs]$Configs"

In the following task, I am running a PowerShell script.

          - task: PowerShell@2
            displayName: 'myTask'
            inputs:
              targetType: 'filePath'
              filePath: 'sourcesmyScript.ps1'
              pwsh: true

I’m using the variable in my script like this:

$env:Configs
[Configs]$envConfigs = ConvertFrom-Json -InputObject $env:Configs -ErrorAction Stop

The Configs is a class that is being imported at the top of the script like so Using module .Configs.psm1. I know it’s being read because if it wasn’t the error would be about a missing type.

Configs.psm1

class Configs
{
    [string[]]$FirewallGroupsToEnable
    [string]$MemoryStartupBytes
}

This is what I get in the pipeline.

##[debug]Processed: ##vso[task.setvariable variable=Configs]@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}

@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}
Cannot convert the "@{FirewallGroupsToEnable=System.Object[]; MemoryStartupBytes=3GB}" value of type "System.String" to type "Configs".

I’ve always casted a deserialized JSON into custom types like this and it always worked. But right now there is something wrong!

I tried to remove ConvertFrom-Json while serializing the JSON (before setting it as a variable) but it doesn’t serialize it right. It shows like this in the pipeline:

enter image description here

It looks like it’s only getting the first curly braces!

So, how do I serialize a JSON regardless of its depth into the pipeline to be used in later tasks inside a script file?

2

Answers


  1. Chosen as BEST ANSWER

    As it turns out, you can't Write-Host multi-line variables even if they were strings due to the fact of PowerShell formatting. Hence, I had to ConvertFrom-Json and then ConvertTo-Json -Compress to get my string in one line.

    $Configs= Get-Content -Path $(Build.SourcesDirectory)sourcesmyJson.json -Raw | ConvertFrom-Json | ConvertTo-Json -Compress


  2. You could pass the json data via a file, here is my YAML snippet that can pass the full JSON content successfully in next task.

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $Configs= Get-Content -Path $(Build.SourcesDirectory)wit.json
          $Configs | Out-File Test.json
    
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $Input = Get-Content Test.json
          Write-Host $Input
    

    Here are similar ticket about pass Json variable for reference.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search