首页 > 解决方案 > Powershell中的嵌套json循环

问题描述

我正在尝试使用 Powershell 中的 JSON 模式在 datalake 中创建嵌套文件夹。

尽管它可以工作,但它会为所有路径创建所有嵌套文件夹。我添加了测试 json。

我的杰森=

{
  "system": [
    {
      "filesystem": "ing",
      "foldersToCreate": [
        {
          "path": "path1/",
          "foldersToCreate": [
            {
              "path": "test1/"
            },
            {
              "path": "test2/"
            }
          ]
        },
        {
          "path": "path2/",
          "foldersToCreate": [
            {
              "path": "test2/"
            }
          ]
        }
      ]
    }
  ]
}

前任。 path1 -test1 -test2 Path2 -test2

代码正在运行,但也在 path1 中创建 test2。我想他们是属性的问题。

function Read-JsonConfig([string]$path)
{
  $file = Get-Item $path
  $plaintext = $file | Get-Content

  $blockComments = '/\*(.*?)\*/'  

  $psObject = $cleantext | ConvertFrom-Json

  return $psObject
}


function create-gen2-folders($folderConfig, $parentPath = '/')
{
  $foldersToCreate = $folderConfig.foldersToCreate | Sort-Object { $_.path }

  foreach($folderToCreate in $foldersToCreate)
  {
    $path = build-path $parentPath $folderToCreate.path

    if($foldersToCreate.foldersToCreate -ne $null)
    {
      create-gen2-folders -folderConfig $folderToCreate -parentPath $path
    }
  }
}

标签: powershell

解决方案


推荐阅读