Below is the vault output generated by the vault command
vault kv get kv/Env | jq
{
"request_id": "8498e5e4-050a-51e9-deaf-64a06a0",
"lease_id": "",
"lease_duration": 0,
"renewable": false,
"data": {
"data": {
"test": "test123",
"test1": "test1234"
},
"metadata": {
"created_time": "2022-11-17T12:2214.93229792Z",
"custom_metadata": null,
"deletion_time": "",
"destroyed": false,
"version": 1
}
},
"warnings": null
}
I am unable to find a way to get only test and test1 and set as environmental variable in powershell. The issue is I would like to extract only test and test1, but it is under data/data which is like sublist and unable to find a way to extract it. Kindly help
I had provided the paths to delete as below but not sure if its efficient way to loop through
vault kv get kv/Env | jq 'del(.data.metadata, .request_id, .lease_id, .lease_duration, .renewable, .warnings)'
I would like to get the output something like below which I can loop through and set as variable.
test=test123
test1=test1234
CodePudding user response:
You can acess the nested object with .data.data. You can convert an object into a list of key-value pairs with to_entries. Then it is only a matter of joining:
vault ... | jq -r '.data.data | to_entries[] | join("=")'
Output:
test=test123
test1=test1234
CodePudding user response:
To provide an all-PowerShell alternative to knittl's helpful jq answer, which also addresses the aspect of setting environment variables:
(vault kv get kv/Env | ConvertFrom-Json).data.data.psobject.Properties |
% { Set-Item Env:$($_.Name) $_.Value }
ConvertFrom-Jsonparses the input JSON into an object graph (based on type[pscustomobject]), so that regular OO dot notation can be used to drill down into the graph, such as with.data.datain this case.The
.Propertiescollection of the intrinsicpsobjectproperty is then used to reflect on the properties of the object contained in.data.data%(a built-in alias of theForEach-Objectcmdlet) then processes each property and usesSet-Itemwith theEnv:drive (representing all in-process environment variables) to set an environment variable for each property's name and value.
