【问题标题】:How to Get-Content of terraform (.tf) file using PowerShell如何使用 PowerShell 获取 terraform (.tf) 文件的内容
【发布时间】:2019-07-30 05:15:52
【问题描述】:

如何使用 PowerShell 获取 Terraform .tf 文件的数据? 就像我们用于 XML 文件一样:

(Get-Content -Path "C:\Desktop\strings.xml") -as [xml]

还有其他获取 terraform 文件的方法吗?

【问题讨论】:

    标签: powershell terraform terraform-template-file


    【解决方案1】:

    Terraform 有自己的configuration language。您需要一个自定义解析器才能将其解析为 PowerShell 数据结构。但是,文档提到 Terraform supports JSON format 作为母语的替代品。您应该能够像这样导入以 JSON 格式编写的配置文件:

    Get-Content 'C:\Desktop\input.tf' | Out-String | ConvertFrom-Json
    

    如果您正在运行 PowerShell v3 或更新版本,则可以这样:

    Get-Content 'C:\Desktop\input.tf' -Raw | ConvertFrom-Json
    

    【讨论】:

    • 这些命令不起作用..给出以下错误 ConvertFrom-Json : Invalid JSON primitive: 。在 line:1 char:89 + ... ari\Desktop\xyz.tf" | Out-String | ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( :) [ConvertFrom-Json], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
    • @Akshay 请实际阅读我的答案,而不是盲目复制/粘贴代码 sn-ps。
    猜你喜欢
    • 2022-12-18
    • 2018-10-28
    • 2016-09-30
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多