Overview of the Issue
The windows-restart provisioner can lose the WinRM session after a reboot when provisioning Azure Windows images.
In our environment, Packer successfully triggers the reboot and the VM comes back online. However, immediately after WinRM reconnects, the build intermittently fails because the provisioner appears to perform additional communication or cleanup against the previous remote PowerShell session, which no longer exists after the reboot.
The failure occurs before the next provisioner starts. The VM itself is healthy and WinRM is available again.
As a workaround, I replaced the windows-restart provisioner with a custom PowerShell-based reboot implementation that schedules the reboot from inside the guest and lets the next PowerShell provisioner handle the reconnect. With this approach, the issue no longer occurs, even after many consecutive reboots.
This suggests the problem is related to the internal implementation of the windows-restart provisioner rather than the reboot itself.
Reproduction Steps
- Build an Azure Windows image using the Azure ARM builder.
- Use the
windows-restart provisioner.
- Repeat multiple reboot cycles during provisioning.
- Eventually, one of the reboot cycles fails after the VM has already restarted successfully.
- WinRM becomes available again, but the build terminates before the next provisioner begins.
The issue is intermittent but reproducible in long-running image builds containing many reboots.
Packer version
Simplified Packer Template
provisioner "windows-restart" {
restart_command = "shutdown /r /f /t 0 /c \"Packer restart\""
restart_timeout = "30m"
check_registry = true
restart_check_command = "powershell -command \"Write-Output 'WinRM Ready'\""
}
The issue also occurs when using longer reboot delays and extended timeouts.
Operating system and Environment details
- Packer v1.12.0
- Azure ARM builder
- Windows 11 Enterprise multi-session (Azure Virtual Desktop image)
- WinRM communicator
- Provisioning executed from a Linux container
- Long-running image build with approximately 18 planned reboots
Log Fragments and crash.log files
The Windows Event Log confirms that the reboot is initiated successfully and the operating system starts normally afterwards.
Event ID 1074
User32
Restart initiated by user "packer"
Event ID 6006
Event Log service stopped
Event ID 109
Kernel-Power
Power Action Reboot
Event ID 6005
Event Log service started
The VM is fully operational after the reboot and WinRM is reachable again.
During the investigation, no scheduled task, MSI installation, Windows Update, or pending reboot was found that could explain the failure.
Additionally, replacing the windows-restart provisioner with a custom PowerShell reboot implementation completely eliminates the issue while using the same VM, communicator, and provisioning sequence.
This strongly suggests that the problem is related to the internal handling of the reboot or remote PowerShell session by the windows-restart provisioner itself.
Overview of the Issue
The
windows-restartprovisioner can lose the WinRM session after a reboot when provisioning Azure Windows images.In our environment, Packer successfully triggers the reboot and the VM comes back online. However, immediately after WinRM reconnects, the build intermittently fails because the provisioner appears to perform additional communication or cleanup against the previous remote PowerShell session, which no longer exists after the reboot.
The failure occurs before the next provisioner starts. The VM itself is healthy and WinRM is available again.
As a workaround, I replaced the
windows-restartprovisioner with a custom PowerShell-based reboot implementation that schedules the reboot from inside the guest and lets the next PowerShell provisioner handle the reconnect. With this approach, the issue no longer occurs, even after many consecutive reboots.This suggests the problem is related to the internal implementation of the
windows-restartprovisioner rather than the reboot itself.Reproduction Steps
windows-restartprovisioner.The issue is intermittent but reproducible in long-running image builds containing many reboots.
Packer version
Simplified Packer Template
The issue also occurs when using longer reboot delays and extended timeouts.
Operating system and Environment details
Log Fragments and crash.log files
The Windows Event Log confirms that the reboot is initiated successfully and the operating system starts normally afterwards.
The VM is fully operational after the reboot and WinRM is reachable again.
During the investigation, no scheduled task, MSI installation, Windows Update, or pending reboot was found that could explain the failure.
Additionally, replacing the
windows-restartprovisioner with a custom PowerShell reboot implementation completely eliminates the issue while using the same VM, communicator, and provisioning sequence.This strongly suggests that the problem is related to the internal handling of the reboot or remote PowerShell session by the
windows-restartprovisioner itself.