简体   繁体   English

如何在使用 Terraform (Azure) 创建 Windows VM 资源时运行 Powershell7 脚本

[英]How do I run a Powershell7 Script upon creation of a Windows VM Resource with Terraform (Azure)

I have an azure windows VM and I want to be able to run a powershell 7 script (powershell 7+ 100% required) upon creation.我有一个 azure windows VM,我希望能够在创建时运行 powershell 7 脚本(需要 powershell 7+ 100%)。 The problem is that none of the vm's come with powershell 7. Do I have any options?问题是 vm 都没有附带 powershell 7。我有什么选择吗? This needs to be fully automated through terraform.这需要通过 terraform 实现完全自动化。

resource "azurerm_virtual_machine_extension" "your-extension" {
  name                 = "${azurerm_windows_virtual_machine.vm[0].name}-extension-name"
  virtual_machine_id   = azurerm_windows_virtual_machine.vm[0].id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  protected_settings = <<SETTINGS
  {
   "commandToExecute": "powershell7 DO-SOMETHING"
  }
  SETTINGS

  depends_on = [
    azurerm_windows_virtual_machine.vm
  ]
}

This might be possiblities you are using an old SKU or image would suggest you upgrade your SKU to sku = "2019-Datacenter" or sku = "2016-Datacenter"这可能是您使用旧 SKU 的可能性,或者图像会建议您将 SKU 升级到sku = "2019-Datacenter"sku = "2016-Datacenter"

 source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }

resource "azurerm_virtual_machine_extension" "example" {
  name                 = "hostname2"
  virtual_machine_id   = azurerm_windows_virtual_machine.example.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.4"

  settings = <<SETTINGS
    {
        "commandToExecute": "powershell.exe -Command \"${local.powershell_command}\""
    }
SETTINGS
  tags = {
    environment = "Production"
  }

  depends_on = [
    azurerm_windows_virtual_machine.example
  ]
}

You can check my this thread which i have answered long back regarding how to install a extenion on Azure Virtual Machine您可以查看我很久以前就如何在 Azure 虚拟机上安装扩展的这个帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接到Azure Windows VM并使用PowerShell运行远程脚本? - How Connect to a Azure Windows VM and run a remote script with PowerShell? 如何在Powershell脚本中将MS Web Deploy安装到Azure Windows Server 2012 R2 VM? - How do I install MS Web Deploy to an Azure Windows Server 2012 R2 VM in a powershell script? Terraform Azure 在 VM 上运行 bash 脚本 - Terraform Azure run bash script on VM 如何使用 Terraform 创建 Azure Windows 虚拟机? - How to create Azure Windows VM using Terraform? 如何使用 powershell 从我的办公室桌面远程重启 Azure VM 上的 windows 服务? - How do I restart a windows service on an Azure VM remotely from my office desktop using powershell? 如何在使用ARM模板的Azure VM部署期间运行PowerShell脚本? - How to run a PowerShell script during Azure VM deployment with ARM template? 我是否需要VM才能在Windows Azure上运行Jira? - Do I need a VM to run Jira on Windows Azure? 如何使用 Terraform Azure CAF 部署 Windows VM? - How to deploy a Windows VM with Terraform Azure CAF? Azure:如何使用通用 Terraform 脚本根据输入自动创建资源组? - Azure: How can the generic Terraform script be used to automate the creation of a resource group based on the input? 创建 VM 时触发 Azure Function - Trigger an Azure Function upon VM creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM