如何从本地和Azure的Terraform中以Azure的方式执行PowerShell文件

时间:2019-07-15 20:16:14

标签: azure powershell terraform

我想从本地和azure的Terraform中执行存储在Azure存储帐户中的PowerShell文件。

我在terraform脚本中包括了Provisioner以执行PowerShell文件

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

我希望执行PowerShell文件。

输出是术语 无法将“ http://MyStorage.blob.core.windows.net/scripts/Import-PowerShell-Modules.ps1”识别为cmdlet,函数,脚本文件或可操作文件的名称 程序。检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。 在第1行:char:1 + http://MyStorage.blob.core.windows.net/scripts/Import-PowerShell ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~     + CategoryInfo:ObjectNotFound:(http://MyStorage...ell-Modules.ps1:String)[],CommandNotFoundException     + FullyQualifiedErrorId:CommandNotFoundException。出现错误。

1 个答案:

答案 0 :(得分:0)

所得到的错误意味着您在local-exec设置程序中使用的命令不是PowerShell命令。

执行在本地存储的PowerShell脚本。您可以使用如下代码:

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

您可以使用文件的相对路径或绝对路径。命令powershell -file显示在这里:

-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.

因此,我建议您是否要执行存储在Azure存储帐户中的PowerShell脚本,最好下载该脚本或在本地创建脚本以下载脚本的内容并执行它。