首页 > 解决方案 > 如何从本地和天蓝色的 terraform 在天蓝色中执行 PowerShell 文件

问题描述

我想从本地和天蓝色的 terraform 执行存储在 Azure 存储帐户上的 PowerShell 文件。

我在我的 terraform 脚本中包含了配置程序来执行 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、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。在 line:1 char:1 + http://MyStorage.blob.core.windows.net/scripts/Import-PowerShell ... + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ( http://MyStorage...ell-Modules.ps1:String ) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException。收到错误。

标签: azurepowershellterraform

解决方案


您收到的错误意味着您在 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 脚本,你最好下载脚本或在本地创建一个脚本来下载脚本的内容并执行它。


推荐阅读