Add windows env variables to json object using powershell
Clash Royale CLAN TAG#URR8PPP
Add windows env variables to json object using powershell
I'm quite new to powershell and just need it for a small task so please excuse my complete and utter ineptitude for the language. I was wondering if it were possible to form a json object based off environment variables and a variable that has already been declared earlier in my script. The variable that was already declared is based off a json config named optionsConfig.json
and the contents of that file are here.
optionsConfig.json
"test1": ["options_size", "options_connection", "options_object"],
"test2":["options_customArgs", "options_noUDP", "options_noName"]
The purpose of the $Options
variable in the code below is to take each element in the list value for the respective test and assume that those elements are environment variables in the system, then find their values and form a dictionary object that will be used in the json.
$Options
Here is what I have so far.
# Read the JSON file into a custom object.
$configObj = Get-Content -Raw optionsConfig.json |
ConvertFrom-Json
# Retrieve the environment variables whose
# names are listed in the $env:test property
# as name-value pairs.
Get-Item -Path env:* -Include $configObj.$env:testTool
$Options = Get-Item -Path env:* -Include $configObj.$env:testTool |
% $hash = @ $hash[$_.Name]=$_.Value $hash
The $Options
variable looks like so when converted to json
$Options
"options_size": "default",
"options_object": "forward open",
"options_connection": "connected"
I have a few other environment variable values that I would like to be a part of the json object. Those 3 other environment variables I would like the value of are listed below.
$Env.testTool = "test1"
$Env.RecordName = "Record1"
$Env.Target = "Target1"
How would I construct a powershell statement to get the json object to be formatted like this? -
data = "test": $Env.testTool, "target": "$Env.Target",
"options": "$Options", "RecordName': "$Env.RecordName"
The keys are all predefined strings and $Options is the dict object from up above. Am I able to form a Json object like this in powershell and how would it be done? Any help would be appreciated. This appears to be the last step in my struggle with powershell.
Here is what I have done.
$jObj = [ordered]@test= $Env:testTool
When I change this variable to $jObj = [ordered]@test= $Env:testTool,options= $Options
I get an error saying missing expression after ','
$jObj = [ordered]@test= $Env:testTool,options= $Options
missing expression after ','
2 Answers
2
When I change this variable to $jObj = [ordered]@test= $Env:testTool,options= $Options
I get an error saying missing expression after ','
$jObj = [ordered]@test= $Env:testTool,options= $Options
missing expression after ','
Entries of a hashtable literal (@ ...
or, in its ordered form, [ordered] @ ...
) must be separated:
@ ...
[ordered] @ ...
either by newlines (each entry on its own line)
or by ;
if placed on the same line.
;
Thus, the following literals are equivalent:
# Multiline form
@
test= $env:testTool
RecordName= $env:RecordName
Target= $env.Target
options=$Options
# Single-line form; separator is ";"
@ test= $env:testTool; RecordName= $env:RecordName; Target= $env.Target; options=$Options
Get-Help about_Hashtables
has more information.
Get-Help about_Hashtables
$jObj = @test= $env:testTool
RecordName= $env:RecordName
Target= $env.Target
options=$Options
$jObj | ConvertTo-Json | Set-Content jsonStuff.json
JsonStuff.json is the new json file for the new json object. This syntax for forming $jObj
seems to have done the trick.
$jObj
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you sir. I changed to the newlines yesterday and it had seemed to work so good to know that it was a valid fix.
– UCProgrammer
Aug 8 at 11:00