1

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.

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked. Got mixed up with using the '"${}"' vs "${}".
Glad to be of Help @MWOITC :)

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.