[英]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. 我根本不熟悉powershell,只需要一些命令。 I was hoping that someone could assist me.
我希望有人可以帮助我。 I have a json file named
optionsConfig.json
that reads like so, 我有一个名为
optionsConfig.json
的json文件,如下所示:
{
"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. 在我的powershell文件中,到目前为止我有一行,这是为了获取json文件的内容。
$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. 我曾计划在我的系统上创建一个名为
test
的环境变量,其值为test1
或test2
,然后我将查看json文件中相关值的列表元素。 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
. (python中的列表理解在这里是完美的。)我是一个python的家伙所以我不太确定如何在powershell中显示这个,但是我说我做了
env = getEnvironmentVariable(test)
并且等于test1
。 I would then say something like for i in $rawData.env return getEnvironmentVariable(i)
然后我会
for i in $rawData.env return getEnvironmentVariable(i)
说一些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. 我非常想要另一个列表或新的json对象,其中包含我们从原始json对象获取的假定环境变量的值。
Is there a way that I can do this in powershell? 有没有办法在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. (编辑)我看到
$rawData.Get-ChildItem Env:test
不起作用。 Is there someone to write something like this in order to get the correct list from json file? 是否有人写这样的东西,以便从json文件中获取正确的列表?
Also setting a new variable like so $var1 = Get-ChildItem Env.tool
and doing $rawData.$test.value
has no effect either. 还设置一个新的变量,如
$var1 = Get-ChildItem Env.tool
和$rawData.$test.value
也没有效果。
# 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: 以上产生了一个
[System.Collections.DictionaryEntry]
实例数组,每个实例代表一个环境变量的名称和值:
Name Value
---- -----
options_connection conn1
options_object obj1
options_size size1
To convert the above to JSON : 要将上面的内容转换为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. 注意:看似冗余的
Select-Object
调用对于剥离PowerShell在幕后添加的其他属性是必要的,否则这些属性将显示在生成的JSON中。
If you wanted to rename the properties in the resulting JSON, you'd need Select-Object
anyway, using calculated properties . 如果要在生成的JSON中重命名属性,则无论如何都需要使用计算属性来
Select-Object
。
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
我没有看到问题,只需设置并使用
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.