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
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
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:-
Output:-
Azure DevOps Powershell Task:-
Output:-