0
votes

I am wanting to execute a PowerShell file stored on Azure storage account from terraform both from locally and from azure.

I included provisioner on my terraform script to execute the PowerShell file

resource "azurerm_automation_account" "my_acc" {

  name                = "AUT-ACCOUNT-586"
  location            = "${azurerm_resource_group.rg_obj.location}"
  resource_group_name = "${azurerm_resource_group.rg_obj.name}"
  sku_name  = "Basic"  

   provisioner "local-exec" {
    command = "http://${azurerm_storage_account.storageaccount.name}.blob.core.windows.net/${azurerm_storage_container.blobstorage.name}/Import-PowerShell-Modules.ps1"
    interpreter = ["PowerShell","-Command"]
  }
}

I expect the PowerShell file to get executed.

The output is The term 'http://MyStorage.blob.core.windows.net/scripts/Import-PowerShell-Modules.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + http://MyStorage.blob.core.windows.net/scripts/Import-PowerShell ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (http://MyStorage...ell-Modules.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException. Getting an error.

1

1 Answers

1
votes

The error which you got means the command that you use in the local-exec provisioner is not PowerShell command.

To execute the PowerShell script which stores locally. You can use the code like this:

provisioner "local-exec" {
        command = "powershell -file ./test.ps1"
    }

You can use the relative path or absolute path of the file. And the command powershell -file shows here:

-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.

So I would suggest if you want to execute the PowerShell script which stores in the Azure Storage account, you would better download the script or create a script locally to download the content of the script and execute it.