Add Windows VM to Domain Using Packer

packerterraform

I'm trying to create Windows images for usage on Azure using Packer. As part of this process, I need to put some files, which are located on an existing VM in Azure, in the image. My idea was to add the VM to the domain, so that I can copy the files directly. However, so far I haven't been able to add the image to the domain.

I've tried running a powershell script, but this gives me the following error message:

Error uploading ps script containing env vars: Error uploading file to $env:TEMP

Next, I've tried running the "Add-Computer" command directly from a powershell-provisioner (both regular and with an elevated user), but neither option was successfull. With the regular shell, it seems as if Packer is just stuck. Nothing happens after the message Provisioning with Powershell. The elevated shell gives me an error message:

(12,8):UserId:
==> azure-arm.xxxxx: At C:\Windows\Temp\packer-elevated-shell-617ab2e4-e084-8ef8-f9f6-0d66a82a0129.ps1:60 char:1

Does anyone know how to add an image to the domain? Or is there another way that I can copy the files from the existing VM to the image? Or perhaps it is best to copy the files during deployment using Terraform?

Best Answer

I haven't been able to solve this via Packer. However, I found a solution using azurerm_virtual_machine_extension where I execute a powershell script which does this as well as some other tasks:

resource "azurerm_virtual_machine_extension" "vm_extension_install_srv" {
    count = var.app_count

  name                       = "vm_extension_install_srv"
  virtual_machine_id         =     azurerm_windows_virtual_machine.app[count.index].id
  publisher                  = "Microsoft.Compute"
  type                       = "CustomScriptExtension"
  type_handler_version       = "1.8"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
        "commandToExecute": "powershell.exe -Command \"C:/setup/provision/first_boot.ps1 -index ${count.index  + 1}; exit 0;\""
    }
SETTINGS
}
Related Topic