MWOITC
MWOITC

Reputation: 13

Local-exec using powershell not executing commands

Having some issues when trying to run Powershell commands through a null-resource 'local-exec". I’m trying to run a PowerShell command with some additional parameters:

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
}

Only when I hardcode the values the Az command executes.

Upvotes: 1

Views: 1490

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11451

I tested the same with something like below :

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

Output:

enter image description here

Note :

I am using Terraform v1.1.0 on windows_amd64

  • provider registry.terraform.io/hashicorp/azurerm v2.90.0
  • provider registry.terraform.io/hashicorp/null v3.1.0

Upvotes: 1

Related Questions