使用带点符号的变量编辑/访问 Powershell 对象 [英] Editing/Accessing Powershell objects using variables with dot notation

查看:28
本文介绍了使用带点符号的变量编辑/访问 Powershell 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 PowerShell 中编辑通过导入 JSON 文件创建的对象.然后我尝试使用点表示法来访问或修改对象的属性.

I'm trying to edit an object in PowerShell that was created through importing a JSON file. I'm then trying to use dot notation to access or amend properties of the object.

例如,下面是一个 JSON 示例和一个代码片段

For example, below is a JSON sample and a code snippet

{
  "menus": {
    "menu1": {
        "position": "left"
    }
  }
}

# Import the file
$settings = Get-Content -Path somefile.json | ConvertFrom-Json

# Specify the property to change
$valueToChange = "menus.menu1.position"
$value = "left"

# Set the appropriate value according to the variables above
$settings.$valueToChange = $value

以上给了我:

异常设置menus.menu1.position":在此对象上找不到属性‘menus.menu1.position’.请验证该属性是否存在并且可以设置."

Exception setting "menus.menu1.position": "The property 'menus.menu1.position' cannot be found on this object. Verify that the property exists and can be set."."

然而,如果我在没有点表示法中的变量的情况下做完全相同的事情,它就可以工作.

Whereas, if I do the exact same without the variables in the dot notation, it works.

# Import the file
$settings = Get-Content -Path somefile.json | ConvertFrom-Json

# Specify the property to change
$value = "left"

# Set the appropriate value according to the variables above but with a hard coded dot notation
$settings.menus.menu1.position = $value

我猜它与作为字符串或类似变量的变量有关,但问题是我需要以编程方式计算点符号以设置文件中的值,或者至少允许用户指定它.

I'm going to guess that it's related to the variable being a string or similar but the issue is that I need to programmatically calculate the dot notation for setting the values in the file, or at least allow a user to specify it.

我现在实际上已经解决了这个问题(只是完全避免了这种方法),但我想看看这是否可行.

I've actually worked around the issue now (by just avoiding this approach entirely) but I wanted to see if this was possible.

即使是点表示法的干净替代品也很好看.我首先使用它的唯一原因是因为我真正的 JSON 对象非常复杂,而且点符号非常简单易懂.

Even a clean alternative to dot notation would be good to see. The only reason I went with it in the first place is because my real JSON object is quite complicated and the dot notation is very straightforward and easy to follow.

推荐答案

除非您使用 Invoke-Expression(即 强烈不鼓励).

What you're trying to do there is not possible in PowerShell unless you use Invoke-Expression (which is strongly discouraged).

您可以做的是使用递归函数,例如像这样:

What you could do is use a recursive function, e.g. like this:

function Set-Property($Json, $Path, $Value) {
    $first, $rest = $Path
    if ($rest) {
        Set-Property -Json $Json.$first -Path $rest -Value $Value
    } else {
        $Json.$first = $Value
    }
}

$valueToChange = 'menus.menu1.position'.Split('.')
$newValue = 'left'

Set-Property -Json $settings -Path $valueToChange -Value $newValue

请注意,这只是一个非常基本的草稿,没有任何错误处理.

Note that this is just a very basic draft with no error handling whatsoever.

这篇关于使用带点符号的变量编辑/访问 Powershell 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆