Newcomer looking to automate maintaining physical machine builds with packer

Hi there,

Disclaimer first up - I am a developer by trade, so automation with packer is very new to me. Please be kind! :slight_smile: I’m looking for a solution to assist my team with spending less time manually keeping windows 10 images up-to-date with patches, and manually installing their scripts etc.

I settled on using packer to create a hyper-v VM from a windows 10 ltsc .iso file, automate adding files, creating folders, running scripts, installing software etc, then once I have an output (e.g. a .box file) use that to somehow convert to a bootable image that can be used to install Windows 10 (with custom additions) onto a physical PC.

I am using the packer files contained in this github repo: GitHub - StefanScherer/packer-windows: Windows Templates for Packer: Win10, Server 2016, 1709, 1803, 1809, 2019, 1903, 1909, 2004, Insider with Docker

I have 3 questions that I was hoping someone could assist with.

  1. At the moment, my configuration file has an ‘output’ of a .box file. Will that be usable to convert into something that could boot on a PC via USB or similar?

  2. Is there a way to configure packer to maintain the vhdx (hyper-v) image file after the process completes as it seems to detach and delete the VM after the process is finished (either successfully or unsuccessfully).

  3. I seem to continue to run into an issue where the packer build hangs at ‘waiting for winrm’ (this is after applying windows updates). Can anyone suggest what might cause this behavior? The packer output is shown below.

PS C:\packer> .\packer build --only hyperv-iso -var 'hyperv_switchname=Intel(R) 82574L Gigabit Network Connection - Virt
ual Switch' -var 'iso_url=./ltscwin10.iso' .\windows_10.json
hyperv-iso: output will be in this color.

==> hyperv-iso: Creating build directory...
==> hyperv-iso: Retrieving ISO
==> hyperv-iso: Trying ./ltscwin10.iso
==> hyperv-iso: Trying ./ltscwin10.iso?checksum=sha256%3A668fe1af70c2f7416328aee3a0bb066b12dc6bbd2576f40f812b95741e18bc3a
==> hyperv-iso: ./ltscwin10.iso?checksum=sha256%3A668fe1af70c2f7416328aee3a0bb066b12dc6bbd2576f40f812b95741e18bc3a => C:/packer/ltscwin10.iso
==> hyperv-iso: Creating floppy disk...
    hyperv-iso: Copying files flatly from floppy_files
    hyperv-iso: Copying file: ./answer_files/10/Autounattend.xml
    hyperv-iso: Copying file: ./floppy/WindowsPowershell.lnk
    hyperv-iso: Copying file: ./floppy/PinTo10.exe
    hyperv-iso: Copying file: ./scripts/fixnetwork.ps1
    hyperv-iso: Copying file: ./scripts/create-shares.ps1
    hyperv-iso: Copying file: ./scripts/disable-screensaver.ps1
    hyperv-iso: Copying file: ./scripts/disable-winrm.ps1
    hyperv-iso: Copying file: ./scripts/enable-winrm.ps1
    hyperv-iso: Copying file: ./scripts/microsoft-updates.bat
    hyperv-iso: Copying file: ./scripts/win-updates.ps1
    hyperv-iso: Done copying files from floppy_files
    hyperv-iso: Collecting paths from floppy_dirs
    hyperv-iso: Resulting paths from floppy_dirs : []
    hyperv-iso: Done copying paths from floppy_dirs
==> hyperv-iso: Creating switch 'Intel(R) 82574L Gigabit Network Connection - Virtual Switch' if required...
==> hyperv-iso:     switch 'Intel(R) 82574L Gigabit Network Connection - Virtual Switch' already exists. Will not delete on cleanup...
==> hyperv-iso: Creating virtual machine...
==> hyperv-iso: Enabling Integration Service...
==> hyperv-iso: Mounting os dvd drive C:/packer/ltscwin10.iso ...
==> hyperv-iso: Mounting floppy drive...
==> hyperv-iso: Skipping mounting Integration Services Setup Disk...
==> hyperv-iso: Mounting secondary DVD images...
==> hyperv-iso: Configuring vlan...
==> hyperv-iso: Determine Host IP for HyperV machine...
==> hyperv-iso: Host IP for the HyperV machine: 192.168.0.24
==> hyperv-iso: Attempting to connect with vmconnect...
==> hyperv-iso: Starting the virtual machine...
==> hyperv-iso: Waiting 6m0s for boot...
==> hyperv-iso: Typing the boot command...
==> hyperv-iso: Waiting for WinRM to become available...

For question 3, I did a detailed log of packer and found wimrm is failing due to this?

2021/07/07 19:39:46 packer.exe plugin: [ERROR] WinRM connection err: http response error: 401 - invalid content type
2021/07/07 19:39:58 packer.exe plugin: [INFO] Attempting WinRM connection...
2021/07/07 19:39:58 packer.exe plugin: [DEBUG] connecting to remote shell using WinRM
2021/07/07 19:39:58 packer.exe plugin: [ERROR] connection error: http response error: 401 - invalid content type
2021/07/07 19:39:58 packer.exe plugin: [ERROR] WinRM connection err: http response error: 401 - invalid content type
2021/07/07 19:40:09 packer.exe plugin: [INFO] Attempting WinRM connection...
2021/07/07 19:40:09 packer.exe plugin: [DEBUG] connecting to remote shell using WinRM
2021/07/07 19:40:09 packer.exe plugin: [ERROR] connection error: http response error: 401 - invalid content type
2021/07/07 19:40:09 packer.exe plugin: [ERROR] WinRM connection err: http response error: 401 - invalid content type
2021/07/07 19:40:20 packer.exe plugin: [INFO] Attempting WinRM connection...
2021/07/07 19:40:20 packer.exe plugin: [DEBUG] connecting to remote shell using WinRM
2021/07/07 19:40:20 packer.exe plugin: [ERROR] connection error: http response error: 401 - invalid content type
2021/07/07 19:40:20 packer.exe plugin: [ERROR] WinRM connection err: http response error: 401 - invalid content type
2021/07/07 19:40:32 packer.exe plugin: [INFO] Attempting WinRM connection...

But no idea how to solve it.

Error solved - incorrect user/password between config files in the GitHub repo I pulled down.