1

I am trying to use Terraform's external provider to run a PowerShell script.

I am not able to pass parameters to the script from Terraform.

I am using the following code to run the script:

variable "file_path" {
  type        = "string"
  description = "Enter the powershell file path"
  default     = "../../Services/azurerm_iam_role/Powershell_Scripts/test.ps1"
}

data "external" "powershell_test" {
  program = ["Powershell.exe", "${var.file_path}"]

  query = {
    foo = "asdf"
    bar = "Hardcoded"
  }
}

The following is my PowerShell script file code:

# Read stdin as string
$jsonpayload = [Console]::In.ReadLine()

# Convert to JSON
$json = ConvertFrom-Json $jsonpayload

# Access JSON values
$foo = $json.foo
$bar = $json.bar

while executing terraform apply I got the following error:

command "Powershell.exe" produced invalid JSON: unexpected end of JSON input

Is there a fix or alternate solution?

1 Answer 1

2

A data source needs to output something which you don't currently appear to be doing. The external data source in particular needs JSON output on stdout which could then be accessed under the result attribute of the data source.

So to continue your example PowerShell script you might want something like this (untested):

# Read stdin as string
$jsonpayload = [Console]::In.ReadLine()

# Convert to JSON
$json = ConvertFrom-Json $jsonpayload

# Access JSON values
$foo = $json.foo
$bar = $json.bar

# Set foobar based on foo and bar
$foobar = "$foo$bar"

# Return foo and foobar
@{
    foobar=$foobar;
    foo=$foo;
} | ConvertTo-Json
Sign up to request clarification or add additional context in comments.

5 Comments

I would definitely use ConvertTo-Json instead of just writing a String to the Ouptut. It could get malformed, depending on the variables content.
@Clijsters I know basically nothing about Powershell, having avoided Windows for the last 5 years as much as possible. I had planned on using CovertTo-Json in the answer but had no way to test it and the online IDE at tio.run/# was giving me issues. If you can suggest an edit that makes the answer better than I'd be grateful.
@ydaetskcoR i have tried ur solution and i am getting the following error Error: Error refreshing state: 1 error(s) occurred: * data.external.powershell_test: 1 error(s) occurred: * data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character ',' looking for beginning of value
@ydaetskcoR PowerShell (core) is vailable on mostly any human-interactable operating system I can think of. U might want to give it a try ;) Neverthless your answer addresses the problem and is totally valid. I'll give it a small edit.
@KailashP: Please try the edited example. Some quote marks weren't escaped.

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.