如何在 Powershell 中 append/update 多深度对象?

How to append/update multidepth object in Powershell?

我无法理解如何在对象中追加或更新数据

所以假设我有一个对象,其数据如下所示,我想在运行此命令

之后添加到架构中的对象架构
$testdata | Format-Custom -Property * -Depth 6
class PSCustomObject
{
  sessions = 
    [
      class PSCustomObject
      {
        type = current
        generated = 2017-08-31T09:02:55.251Z
        windows = 
          [
            class PSCustomObject
            {
              id = 770
              incognito = False
              tabs = 
                [
                  class PSCustomObject
                  {
                    active = True
                    id = 771
                    incognito = False
                    title = Subscriptions - YouTube
                    url = https://www.youtube.com/feed/subscriptions
                    windowId = 770
                  }
                  class PSCustomObject
                  {
                    active = False
                    id = 776
                    incognito = False
                    title = Overly Sarcastic Productions - YouTube
                    url = https://www.youtube.com/user/RedEyesTakeWarning/videos
                    windowId = 770
                  }

                ]

            }
            class PSCustomObject
            {
              id = 773
              incognito = False
              tabs = 
                [
                  class PSCustomObject
                  {
                    active = False
                    id = 774
                    incognito = False
                    title = Technology - Google News
                    url = https://news.google.com/news/headlines/section/topic/TECHNOLOGY?ned=us&hl=en
                    windowId = 773
                  }
                  class PSCustomObject
                  {
                    active = False
                    id = 806
                    incognito = False
                    title = Microsoft PowerShell Is a Hot Hacker Target, But Its Defenses Are Improving | WIRED
                    url = https://www.wired.com/story/microsoft-powershell-security/
                    windowId = 773
                  }
                  ...
                ]
            }
          ]
      }
    ]
}

假设我想向 windows 添加一个新标签,ID 为 770 新标签的 title 将是 yahoourl 将是 https://yahoo.comid 将是 9001

我如何才能将新条目添加到选项卡中而不必重新创建整个对象?

所以这并不是真正的答案,而更像是一种解决方法。

我自己不明白如何在 Powershell 中按照您的意愿修改对象。通常一个简单的 += 通常适用于大多数事情。

但是,对于您的问题,它似乎试图在具有特定 ID 的特定对象中直接添加具有键值对的对象 += 是行不通的,因为您正在尝试解析你的方式。你只会得到 属性 not being found 之类的愚蠢错误。

如果它是一个静态列表并且 order/position 在我更新它之前不会改变,我的方法是获取特定对象的 INDEX我想补充一下。然后对您的数据 object/hash 做一个简单的 += 到您希望根据索引修改的任何对象。

喜欢以下内容:

首先,使用键值对准备新对象或哈希。

 $newhash = @{}
 $newhash.Add("title","yahoo") 
 $newhash.Add("url", "https://yahoo.com")
 $newhash.Add("id", "9001")

接下来,用.IndexOf()找到你想要modify/add-to的对象的索引,比如你的id 770

$position = $testdata.sessions.windows.id.IndexOf(770)

这应该告诉您具有特定 id 770 的对象在对象列表中的位置

现在,从字面上看,对您想要 add/modify 的特定 window 的选项卡执行简单的 +=

$testdata.sessions.windows[$position].tabs += $newhash

如果您感到困惑,在某些内容之后使用 [] 表示选择它在列表中的位置,例如 [0] 是第一个,依此类推。

应该是这样。

注意:这不适用于在您更新数据之前更新数据并且在您修改它之前位置发生变化的情况。

我知道这可能不是你要找的,但仍然。

希望这对您有所帮助。