│ 运行命令"powershell -file ./main.ps1"时出错:exec: "PowerShell" : │ $PATH中找不到可执行文件。输出:



我正在通过azure devops管道实现terraform。但需要很少的配置通过Powershell脚本。为此,我编写了以下空资源和约束符:

resource "null_resource" "terraform-script" {
provisioner "local-exec" {
command = "powershell -file ./main.ps1"
interpreter = ["PowerShell", "-Command"]
}
depends_on = [
azurerm_resource_group.rg, azurerm_kubernetes_cluster.kc
]
}

都是主要的。tf和其他tf文件位于我的powershell脚本main.ps1所在的目录中。但我收到的错误是我的路径不正确。我想知道我做错了什么,以及如何解决这个问题。

我认为您在问题中显示的提供程序块是使用powershell来运行powershell,通过构建这样的命令行:

PowerShell -Command "powershell -file ./main.ps1"

因为你的脚本已经在一个单独的文件中,我认为直接使用-File而不是-Command来执行它会更直接,像这样:

provisioner "local-exec" {
command = "./main.ps1"
interpreter = ["PowerShell", "-File"]
}
上面的命令将运行如下命令:
PowerShell -File "./main.ps1"

只要main.ps1在你当前的工作目录下,那就应该工作,但是如果你把你的PowerShell脚本作为一个文件包含在你的Terraform模块的同一目录下,那么你需要指定相对于模块目录的路径,像这样:

provisioner "local-exec" {
command = "${path.module}/main.ps1"
interpreter = ["PowerShell", "-File"]
}

path.module是一个Terraform表达式,它返回从当前工作目录到包含provisioner块的文件所在目录的相对路径。

我得到了自己问题的答案,下面是解决方案:

resource "null_resource" "testing-script3" {
provisioner "local-exec" {
command     = ".'${path.module}\main.ps1' "
interpreter = ["pwsh", "-Command"]
}
depends_on = [ azurerm_resource_group.rg,           azurerm_kubernetes_cluster.kc]
}

最新更新