Terraform:命令“Powershell.exe"产生了无效的 JSON:JSON 输入的意外结束 [英] Terraform: command "Powershell.exe" produced invalid JSON: unexpected end of JSON input

查看:15
本文介绍了Terraform:命令“Powershell.exe"产生了无效的 JSON:JSON 输入的意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Terraform 的外部提供程序来运行 PowerShell 脚本.

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

我无法从 Terraform 向脚本传递参数.

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"
  }
}

以下是我的PowerShell脚本文件代码:

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

在执行 terraform apply 时出现以下错误:

while executing terraform apply I got the following error:

命令Powershell.exe"产生无效的 JSON:JSON 意外结束输入

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

是否有修复或替代解决方案?

Is there a fix or alternate solution?

推荐答案

一个数据源需要输出一些你目前似乎没有做的事情.外部数据源 特别需要 JSONstdout 上的输出,然后可以在数据源的 result 属性下访问.

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.

因此,要继续您的示例 PowerShell 脚本,您可能需要这样的内容(未经测试):

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

这篇关于Terraform:命令“Powershell.exe"产生了无效的 JSON:JSON 输入的意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆