Terraform azurerm_virtual_machine_extension,使用 CustomScriptExtension 运行本地 PowerShell 脚本

gsg*_*l76 1 powershell terraform

如何在 terraform azurerm_virtual_machine_extension 中运行本地(不存储到 blob 存储帐户)PowerShell 脚本

文件夹有

  1. 主文件
  2. 安装.ps1

    资源“azurerm_virtual_machine_extension”“软件”{名称=“安装软件”resource_group_name=azurerm_resource_group.azrg.name virtual_machine_id=azurerm_virtual_machine.vm.idpublisher=“Microsoft.Compute”type=“CustomScriptExtension”type_handler_version=“1.9”

      settings = <<SETTINGS
        { 
          "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File \"install.ps1\""
        } 
        SETTINGS
    } 
    
    Run Code Online (Sandbox Code Playgroud)

    但失败了

    [
            {
                "code": "ComponentStatus/StdOut/succeeded",
                "level": "Info",
                "displayStatus": "Provisioning succeeded",
                "message": "Windows PowerShell \r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n"
            },
            {
                "code": "ComponentStatus/StdErr/succeeded",
                "level": "Info",
                "displayStatus": "Provisioning succeeded",
                "message": "The argument 'install.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.\r\n"
            }
        ]
    
    Run Code Online (Sandbox Code Playgroud)

任何铅。

谢谢

gsg*_*l76 6

这对我有用。

resource "azurerm_virtual_machine_extension" "software" {
  name                 = "install-software"
  resource_group_name  = azurerm_resource_group.azrg.name
  virtual_machine_id   = azurerm_virtual_machine.vm.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  protected_settings = <<SETTINGS
  {
    "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf.rendered)}')) | Out-File -filepath install.ps1\" && powershell -ExecutionPolicy Unrestricted -File install.ps1"
  }
  SETTINGS
}

data "template_file" "tf" {
    template = "${file("install.ps1")}"
} 
Run Code Online (Sandbox Code Playgroud)