在Powershell中解析YAML文件

时间:2019-03-19 14:15:01

标签: powershell yaml powershell-v3.0 apiconnect

我想在PowerShell文件中从IBM API Connect解析YAML文件。我将无法放入第三方程序包或DLL,因为安全审查不会同意它。

---
product: "1.0.0"
info:
  name: "api2product"
  title: "API2product"
  version: "1.0.0"
visibility:
  view:
    enabled: true
    type: "public"
    tags: []
    orgs: []
  subscribe:
    enabled: true
    type: "authenticated"
    tags: []
    orgs: []
apis:
  api1:
    $ref: "api1_1.0.0.yaml"
  api2:
    $ref: "api2_1.0.0.yaml"
  api3:
    $ref: "api3_1.0.0.yaml"
  api4:
    $ref: "api4_1.0.0.yaml"
  api5:
    $ref: "api5_1.0.0.yaml"
plans:
  default:
    title: "Default Plan"
    description: "Default Plan"
    approval: false
    rate-limit:
      hard-limit: false
      value: "100/hour"

我有兴趣仅获取与之相关的API YAML文件,对此我进行了搜索并开发了一个示例代码,这些代码实际上正在运行。

$text = Get-Content -Path "C:\Users\abhi\Desktop\projects\TRYG\GitLab\APIPOC\test.yaml"
$regex = '(?ms)apis:(.+?)plans:'
$text = $text -join "`n"
$OutputText = [regex]::Matches($text, $regex) |
              foreach {$_.Groups[1].Value -split $regex}
$OutputText = $OutputText.Replace("`$ref:", "")
$OutputText = $OutputText.Replace(":", "=")
$OutputText = $OutputText.Replace("=`n", "=")
$OutputText = $OutputText.Replace("`"", "")
$AppProps = ConvertFrom-StringData ($OutputText)
$AppProps.GetEnumerator() | ForEach-Object {$_.Value}
[string[]]$l_array = $AppProps.Values | Out-String -Stream

有没有简单的方法来实现这一目标,而不是在字符串中进行多次替换?

0 个答案:

没有答案