Hey, I'm posting this as I wasn't able to find the information elsewhere. Maybe this will help others who might run into the same issue.
I have been using MarcinBojkos packer templates (https://github.com/marcinbojko/hv-packer) for building 2016 Hyper-V templates and are currently in the process of transforming them into a Windows Server 2012 R2 packer template. During my testing I ran into a problem at the end, after the SYSPREP provisioner part had run. Packer failed shutting down the VM, probably because SYSPREP had already shut down the server, the logs does not indicate this. *Log snip:* ==> hyperv-iso: Forcibly halting virtual machine... ==> hyperv-iso: Error stopping VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop. ==> hyperv-iso: At C:\Users\hn_kre\AppData\Local\Temp\powershell181645961.ps1:5 char:5 ==> hyperv-iso: + Hyper-V\Stop-VM -VM $vm -Force -Confirm:$false ==> hyperv-iso: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ==> hyperv-iso: + CategoryInfo : NotSpecified: (:) [Stop-VM], VirtualizationException ==> hyperv-iso: + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StopVM ==> hyperv-iso: Error stopping VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop. ==> hyperv-iso: At C:\Users\hn_kre\AppData\Local\Temp\powershell181645961.ps1:5 char:5 ==> hyperv-iso: + Hyper-V\Stop-VM -VM $vm -Force -Confirm:$false ==> hyperv-iso: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ==> hyperv-iso: + CategoryInfo : NotSpecified: (:) [Stop-VM], VirtualizationException ==> hyperv-iso: + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StopVM ==> hyperv-iso: Step "StepShutdown" failed *The failing provisioner:* { "type": "powershell", "inline": [ "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}", "if (!(Test-Path C:\\Windows\\Packer)) {New-Item -ItemType directory -Path C:\\Windows\\Packer}", "if (Test-Path E:\\unattend.xml) {copy E:\\unattend.xml C:\\Windows\\Packer\\}", "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet /unattend:$Env:SystemRoot\\Packer\\unattend.xml" ] } By comparing the VM sysprep log timestamps with the event log on my hyper-v host builder i noticed that exactly one second after sysprep had shutdown the server, Hyper-V reported a mostly useless hyper-v work error in the "Microsoft-Windows-Hyper-V-Worker/Admin" log: The description for Event ID 3452 from source Microsoft-Windows-Hyper-V-Worker cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer. If the event originated on another computer, the display information had to be saved with the event. The following information was included with the event: win2012r2 79BF6CC5-C059-42C3-BF84-623D1338A6BA %%2147943515 0x8007045B The locale specific resource for the desired message is not present I looked through the packer source code for the shutdown function (https://github.com/hashicorp/packer/blob/master/builder/hyperv/common/step_shutdown.go) and noticed that any error during the VM shutdown process would trigger the "Error stopping VM:" error and packer would start the cleanup process and quit. I figured that the hyper-v shutdown cmdlet would probably error out if the server was already off. *The solution* The solution was simple, just change the sysprep commandline option /shutdown to /quit, then packer was able to successfully shut down the server. For some reason this was not a problem when building Windows Server 2016. -- This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list. GitHub Issues: https://github.com/mitchellh/packer/issues IRC: #packer-tool on Freenode --- You received this message because you are subscribed to the Google Groups "Packer" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/a9ceaac7-eef4-4561-9f75-9a32213f6aeb%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
