10

I want to use json file to store variables used by powershell script. In azure devops I have configured pipeline, pipeline contains powershell script with a list of variables in it. I would like to use json file and store in this file all variables. I want to store json file in Azure repo, and use it once pipeline is started. I dont want to use variable groups. I found this solution [Json to Variable]https://marketplace.visualstudio.com/items?itemName=OneLuckiDev.json2variable&targetId=02fbae5c-cc01-4c8b-a90f-7393a3156347 but I read some uncomplimentary opinions about this task. Is there any method to use json file as a place to store variables, or maybe I can use different file format? UPDATE: enter image description here

riQQ
  • 9,878
  • 7
  • 49
  • 66
tester81
  • 533
  • 2
  • 9
  • 28
  • 1
    What the issue with this extension? try it and after that ask. – Shayki Abramczyk Oct 28 '19 at 21:40
  • 3
    Why not just read the json file with powershell? If you want to set these as environment variables that are used in later steps in your pipeline then `echo` the variables like so: `echo "##vso[task.setvariable variable=a]20"` : https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-in-script – Pero P. Oct 28 '19 at 23:37

1 Answers1

10

Why not directly to use the Powershell built-in cmdlet: ConvertFrom-Json. See this official doc about this cmdlet description: Powershell: ConvertFrom-Json.

You can add the script:

Get-Content "{JSON file path}" | out-string | ConvertFrom-Json

Get-Content would read the JSON file into an array of strings, and ConvertFrom-Json convert these strings into the PSCustomObject object. Then you can call the exactly object with simple leverage dot notation(.), then use it with this format into the task script.

Here is the simple sample can for you refer:

$data = Get-content {your JSON file path}| out-string | ConvertFrom-Json
Write-Output $data.ts

enter image description here

Here I tried in my local Powershell-ISE which is same usage in Powershell task. ts is one of my JSON object. In azure devops, you can use the path like $(Build.SourcesDirectory)\..\..\...json to specified your file location if you store it in Repos.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Hi, That is what I am exactly looking for, I have tested this using pipeline inline script and it works fine. Thank You!! – tester81 Oct 29 '19 at 09:21
  • one more question, why I am still getting error with message "Get-Content : Cannot find path 'D:\Variables.json' because it does not exist.". I tested this solution using pipeline inline script and it worked. Now in my powershell script I am using this --> `$JsonFilePath = "$($Build.SourcesDirectory)\Variables.json" $JsonDataBase = Get-Content $($JsonFilePath) | out-string | ConvertFrom-Json` and facing errors, file is stored in devops repo. – tester81 Oct 29 '19 at 10:24
  • @tester81. According to your script configuration, I assume your `Variable.json` in located in the root path of your repos, right? Change your path script as `$JsonFilePath = "$(Build.SourcesDirectory)\Variables.json" $JsonDataBase = Get-Content $JsonFilePath | out-string | ConvertFrom-Json`. The method you used does not correct, because the correct path value should be `d:\a\1\s\...` instead of `D:\....`. – Mengdi Liang Oct 29 '19 at 10:38
  • OK, I ve just corrected, now I have `$JsonFilePath = "$(Build.SourcesDirectory)\Variables.json"`, but still there is something wrong, now got an error like --> `D:\a\1\s\Get-RbacReport.ps1 : The term 'Build.SourcesDirectory' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.` – tester81 Oct 29 '19 at 11:14
  • @tester81. This is the simple sample on my powershell: https://imgur.com/a/Rc0SVsW .If convenient, could you show me how's your script like? – Mengdi Liang Oct 29 '19 at 11:18
  • I have the same code as Yours, pls have a look at UPDATE in problem description at the beginning. – tester81 Oct 29 '19 at 11:28
  • @tester81. No problem about that script. As I see, in your error message, the unrecognized error caused from the `Get-RbacReport.ps1` file. Does it is the one you defined? For troubleshoting, please just define the variable `$JsonFilePath` when write-host its value, to see whether the path value can be got successfully. – Mengdi Liang Oct 29 '19 at 11:40
  • I have used env variable inside the script --> `$RepoPath = $Env:BUILD_SOURCESDIRECTORY` and it resolved the problem. – tester81 Oct 29 '19 at 15:57