skip to Main Content

I’m working in an Azure Pipelines (not inline) where I’m trying to insert a variable’s value into a JSON file, the mentioned file is download from a storage account and read during the process, so far so good. The files contains something as below

{"MedUnits":[
    {
        "System":"Med",
        "UnitName":"MedicalUnitEast",
        "MedID":"",
        "Assigments":[
        {
            "Measur":"Density",
            "Unit":"m3"
        },
        {
            "Measur":"Weight",
            "Unit":"kg"
        }
    ]}
]}

I need to insert the variable value for the MedID key that is always empty, I’ve been trying different ways (math/replace, if) but I’m getting errors for some cmdlet. This is an example of what I’ve tried:

#Downloading file from storageaccount
$unitsFile = Invoke-WebRequest -URI "https://storage.blob.core.windows.net/folder/${json}?sv=TOKEN"
$JsonData = Get-Content -Path .$unitsFile | ConvertFrom-Json
$JsonData.update | % { if($JsonData.MedUnits.MedID){
                                    $JsonData.MedUnits.MedID= "$ID"
                            }
                        }
$JsonData | ConvertTo-Json -Depth 4  | set-content $unitsFile 

It seems I’m doing something wrong as I’m getting the error "##[error]Get-Content : Illegal characters in path." during pipeline execution, and if I remove the -Path .(backslash) after the Get-Content, I get another error stating ##[error]Get-Content : Cannot find drive. A drive with the name ‘{"MedUnits"’ does not exist.

2

Answers


  1. Chosen as BEST ANSWER

    A couple of days back, I found a similar issue reported a couple of years back and the user found a workaround, this help me move on.

    Powershell : How to get a JSON file content from URI instead of local disk


  2. I have tried below code in my Local and Azure Pipelines Powershell task and the MedID was updated successfully. Make sure the path to your Json file is correct while running your script.

    Local:-

    Script:-

    $unitsFile="C:Logic-appMedunits.json"
    $ID="1233"
    
    $JsonData = Get-Content -Path $unitsFile -Raw | ConvertFrom-Json
    $JsonData.MedUnits[0].MedID = $ID
    $JsonData | ConvertTo-Json -Depth 4 | Set-Content $unitsFile
    

    Output:-

    enter image description here

    enter image description here

    Azure DevOps Powershell Task:-

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $unitsFile="$(System.DefaultWorkingDirectory)/Medunits.json"
          $unitsFile
          $ID="1244"
          
          $JsonData = Get-Content -Path $unitsFile -Raw | ConvertFrom-Json
          $JsonData.MedUnits[0].MedID = $ID
          $JsonData | ConvertTo-Json -Depth 4 | Set-Content $unitsFile
          $JsonData
    

    Output:-

    enter image description here

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