简体   繁体   中英

Use a for loop for list and set element to environment variable then get value

I'm not familiar with powershell at all and only need it for a few commands. I was hoping that someone could assist me. I have a json file named optionsConfig.json that reads like so,

{
"test1": ["options_size", "options_connection", "options_object"],

"test2":["options_customArgs", "options_noUDP", "options_noName"]
}

In my powershell file, I have one line so far and this is to get the content of the json file.

$rawData = Get-Content -Raw -Path "optionsConfig.json" | ConvertFrom-Json

I had planned on having an environment variable on my system named test that would have the value of test1 or test2 and from that, I would look at the elements of the list in the associated value in the json file. With these elements from the list, I would assume that they are also environment variables and I would want to get their values. ( a list comprehension in python would be perfect here).I'm a python guy so I am not quite certain how to show this in powershell but say I did env = getEnvironmentVariable(test) and that equals test1 . I would then say something like for i in $rawData.env return getEnvironmentVariable(i)

I pretty much want another list or new json object with the values for the assumed environment variables that we get from the original json object.

Is there a way that I can do this in powershell? It would help a lot if anyone could assist. I apologize if I wasn't clear with anything. Thanks

(edit) I see that $rawData.Get-ChildItem Env:test does not work. Is there someone to write something like this in order to get the correct list from json file?

Also setting a new variable like so $var1 = Get-ChildItem Env.tool and doing $rawData.$test.value has no effect either.

# Sample values: make environment variable 'test' ($env:test)
# point to property 'test1'
$env:test = 'test1'

# Provide sample values for the environment-variable names listed
# in the 'test1' property.
$env:options_size = 'size1'
$env:options_connection = 'conn1'
$env:options_object = 'obj1'


# 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 ('test1' in this example),
# as name-value pairs.
Get-Item -Path env:* -Include $configObj.$env:test

The above yields an array of [System.Collections.DictionaryEntry] instances each representing an environment variable's name and value:

Name                           Value
----                           -----
options_connection             conn1
options_object                 obj1
options_size                   size1

To convert the above to JSON :

Get-Item -Path env:* -Include $configObj.$env:test |
  Select-Object Name, Value | ConvertTo-Json

Note: The seemingly redundant Select-Object call is necessary to strip additional properties that PowerShell adds behind the scenes, which would otherwise show in the resulting JSON.
If you wanted to rename the properties in the resulting JSON, you'd need Select-Object anyway, using calculated properties .

This yields:

[
  {
    "Name": "options_connection",
    "Value": "conn1"
  },
  {
    "Name": "options_object",
    "Value": "obj1"
  },
  {
    "Name": "options_size",
    "Value": "size1"
  }
]

I don't see a problem, just set and use $ENV:test

$rawdata = Get-Content .\optionsconfig.json | convertfrom-json
$Env:Test="test1"
$rawdata.$ENV:Test

$Env:Test="test2"
$rawdata.$ENV:Test

Sample output

options_size
options_connection
options_object

options_customArgs
options_noUDP
options_noName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM