PowerShell此处-字符串连接到Json

t9aqgxwy  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(216)

我正在尝试将PowerShell这里的字符串转换为Json文件。这是我需要转换的样本大小。我需要转换几十个覆盖文件,这可能是相当长的,手动做这不是我想要的方式完成这一点。

$a = @'
override['abc_os_config']['local_administrators']['default_administrators']['abc.com'] = %w {}
override['abc_os_config']['local_administrators']['default_administrators']['devad.abc.com'] = %w {}
override['abc_os_config']['local_administrators']['default_administrators']['qad.abc.com'] = %w {}
override['CIS_Remediation_Windows']['security_options']['lanman_authentication_level'] = 3
'@

$b = $a.Replace('override', '').Replace("'", '').Replace('[', '\').Replace(']', '').Replace(' =',':')

$b.Split('\') | ConvertTo-Json -Depth 100

生成的内容:

[
  "",
  "abc_os_config",
  "local_administrators",
  "default_administrators",
  "abc.com: %w {}\n",
  "abc_os_config",
  "local_administrators",
  "default_administrators",
  "devad.abc.com: %w {}\n",
  "abc_os_config",
  "local_administrators",
  "default_administrators",
  "qad.abc.com: %w {}\n",
  "CIS_Remediation_Windows",
  "security_options",
  "lanman_authentication_level: 3"
]

应生成的内容:

{
  "abc_os_config": {
    "local_administrators": {
      "default_administrators": {
        "abc.com": "%w {}",
        "devad.abc.com": "%w {}",
        "qad.abc.com": "%w {}"
      }
    }
  },
  "CIS_Remediation_Windows": {
    "security_options": {
      "lanman_authentication_level": "3"
    }
  }
}
jhkqcmku

jhkqcmku1#

下面是一个使用我的函数Set-TreeValue的解决方案,在对原始数据进行了一些预处理之后:


# Create sample input

$a = @'
override['abc_os_config']['local_administrators']['default_administrators']['abc.com'] = %w {}
override['abc_os_config']['local_administrators']['default_administrators']['devad.abc.com'] = %w {}
override['abc_os_config']['local_administrators']['default_administrators']['qad.abc.com'] = %w {}
override['CIS_Remediation_Windows']['security_options']['lanman_authentication_level'] = 3
'@ 

# Split input text into an array of lines

$textLines = $a -split '\r?\n'

# Create an ordered hashtable

$result = [ordered] @{}

foreach( $line in $textLines ) {
    # Split current line into path and value
    $path, $value = $line -split "'\]\s*=\s*"

    # Remove unwanted stuff from the path
    $path = $path -replace "override\['"

    # Add current value to the tree, creating any nested hashtables as necessary
    Set-TreeValue -HashTable $result -Path $path -Value $value -PathSeparator "'\]\['"
}

# Specify a large enough depth as default value of 2 would skip deeper nested data

$result | ConvertTo-Json -Depth 99

输出:

{
  "abc_os_config": {
    "local_administrators": {
      "default_administrators": {
        "abc.com": "%w {}",
        "devad.abc.com": "%w {}",
        "qad.abc.com": "%w {}"
      }
    }
  },
  "CIS_Remediation_Windows": {
    "security_options": {
      "lanman_authentication_level": "3"
    }
  }
}

每行处理方式:

例如乘这条线

override['abc_os_config']['local_administrators']['default_administrators']['abc.com'] = %w {}
  • 将当前行拆分为路径和值:
$path = "override['abc_os_config']['local_administrators']['default_administrators']['abc.com"
$value = "%w {}"
  • 从路径中删除不需要的内容:
$path = "abc_os_config']['local_administrators']['default_administrators']['abc.com"
  • Set-TreeValue中:
  • 将路径拆分成组件,使用']['作为分隔符(反斜杠转义,因为函数需要RegEx)
abc_os_config
local_administrators
default_administrators
abc.com
  • 创建任何嵌套的哈希表并赋值

相关问题