0

How to fetch data of Terraform .tf file using PowerShell? like we use for XML files:

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

Is there is other way to fetch terraform file?

2 Answers 2

0

If you do not mind using an open source project I would suggest converting HCL to JSON by applying hcl2json library. The executable can be downloaded from here: https://github.com/tmccombs/hcl2json/releases

Then you can invoke the process from PowerShell against your .tf or .tfvars file. The outcome should be this:

(& .\hcl2json_windows_amd64.exe -simplify "dev.shared.tfvars" | ConvertFrom-Json).key_vault_settings

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Terraform has its own configuration language. You need a custom parser for being able to parse that into PowerShell data structures. However, the documentation mentions that Terraform supports JSON format as an alternative to the native language. You should be able to import configuration files written as JSON like this:

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

or like this if you're running PowerShell v3 or newer:

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

2 Comments

These commands not working ..giving below error ConvertFrom-Json : Invalid JSON primitive: . At 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 Please actually read my answer instead of blindly copy/pasting code snippets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.