本文介绍了使用Powershell的本地-exec不执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试通过null-resource‘local-exec";运行PowerShell命令时遇到一些问题。我正在尝试使用一些其他参数运行PowerShell命令:
provisioner "local-exec" {
interpreter = ["PowerShell", "-Command"]
command = <<EOT
$ResourceGroupName = '"${module.rg.resource_group.name}"'
$FunctionAppName = '"${var.function_apps[each.key].name}"'
$SubscriptionId = '"${var.subscriptions.id}"'
# Option 1 - does nothing
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
# Option 2 - does nothing
(Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId)
# Option 3 - shows the correct cmd line with correctly expanded variables but does not execute the command
"Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId"
# Option 4 - when I hardcode the values it works
Get-AzFunctionApp -ResourceGroupName "real_rg_name" -Name "real_rg_appname" -SubscriptionId real_subscr_id
EOT
}
仅当我硬编码Az命令执行的值时。
推荐答案
我测试了相同的东西,如下所示:
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "example"{
name = "ansumantest"
}
variable "function_apps" {
default = ["ansumantestfunc1","ansumantestfunc2"]
}
variable "Subscription" {
default = "948d4068-xxxx-xxxx-xxxx-xxxxxxxxxxx"
}
resource "null_resource" "example2" {
count = length(var.function_apps)
provisioner "local-exec" {
command = <<Settings
$ResourceGroupName = "${data.azurerm_resource_group.example.name}"
$FunctionAppName = "${var.function_apps[count.index]}"
$SubscriptionId = "${var.Subscription}"
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
Settings
interpreter = ["PowerShell", "-Command"]
}
}
输出:
注意:
我在WINDOWS_AMD64上使用Terraform v1.1.0
- Provider registry.terraform.io/hashicorp/azurerm v2.90.0
- 提供商registry.terraform.io/hashicorp/null v3.1.0
这篇关于使用Powershell的本地-exec不执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!