问题
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:
回答1:
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
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.
来源:https://stackoverflow.com/questions/58597755/import-azure-devops-pipeline-variables-from-json-file