Packer - Unresponsive after Windows VM reboots via Powershell script provisioner

1.6k views Asked by At

Issue: I cannot get Packer to work when running a powershell script that ends up rebooting the Windows VM after software install.

Upon script completion (scripts/watchMaker_stigVM.ps1), the machine will reboot, and Packer moves on to the next Provisioner. The next provisioner in this case, is the Powershell inline provisioner for sysprep

Packer hangs, then throws an error, and delete the resources for cleanup

Things i have tried:"

1) Using the "Windows-Restart" Packer provisioner after the Powershell script Provisioner which causes the VM to reboot (scripts/watchMaker_stigVM.ps1).

2) Adding in a "restart_check_command" to check for the "RebootPending" registry key

3) Adding in a "Pause_before" after the Powershell Script Provisioner, and before a Windows-Restart provisioner, to give time for the machine to come back online, and allow WinRM to re-connect.

Provisioner with above attempts:

    "provisioners": [
{
  "type": "powershell",
  "scripts": [
  "scripts/watchMaker_stigVM.ps1"
  ]
},
{
  "type": "powershell",
  "pause_before": "240s",
  "inline": ["dir c:\\"]
},
{
  "type": "windows-restart",
  "timeout": "2h",
  "restart_check_command": "HKLM:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending"
},
{
  "type": "powershell",
  "pause_before": "120s",
  "inline": ["dir c:\\"]
},
{
  "type": "powershell",
  "inline": [
    "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
    "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
    "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
  ]
}

] }

Build Script used in above scenario with out any Windows-Restart or Pause provisioners:

    {
  "variables": {
    "client_id": "",
    "client_secret": "",
    "object_id": "",
    "tenant_id": "",
    "subscription_id": "",
    "managed_image_name": "",
    "managed_image_resource_group_name": "",
    "location": "",
    "os_type": "",
    "image_publisher": "",
    "image_offer": "",
    "image_sku": "",
    "dept": "",
    "task": ""
  },
  "builders": [
    {
      "type": "azure-arm",
      "client_id": "{{user `client_id`}}",
      "client_secret": "{{user `client_secret`}}",
      "tenant_id": "{{user `tenant_id`}}",
      "subscription_id": "{{user `subscription_id`}}",
      "managed_image_resource_group_name": "{{user `managed_image_resource_group_name`}}",
      "managed_image_name": "{{user `managed_image_name`}}",
      "os_type": "{{user `os_type`}}",
      "image_publisher": "{{user `image_publisher`}}",
      "image_offer": "{{user `image_offer`}}",
      "image_sku": "{{user `image_sku`}}",
      "communicator": "winrm",
      "winrm_use_ssl": true,
      "winrm_insecure": true,
      "winrm_timeout": "2h",
      "winrm_username": "packer",
      "azure_tags": {
        "dept": "{{user `dept`}}",
        "task": "{{user `task`}}"
      },
      "location": "{{user `location`}}",
      "vm_size": "Standard_B4ms"
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "timeout": "2h",
      "script":
        "scripts/watchMaker_stigVM.ps1"
    },
    {
      "type": "powershell",
      "inline": [
        "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
        "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
        "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
      ]
    }
  ]
}

Packer log, showing the successful completion of the Powershell script "scripts/watchMaker_stigVM.ps1", the next Powershell inline provisioner attempting to run, hanging, then packer failing, and removing resources

    ==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Applying the salt "highstate", states=highstate
==> azure-arm: 2019-11-08 17:36:51,030 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command: C:\Salt\salt-call.bat --local --retcode-passthrough --no-color --config-dir C:\Watchmaker\Salt\conf --log-file C:\Watchmaker\Logs\salt_call.debug.log --log-file-level debug --log-level error --out quiet --return local state.highstate
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: Command retcode: 0
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Salt states all applied successfully!
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Cleanup Time...
==> azure-arm: 2019-11-08 17:38:04,492 [watchmaker.workers.base.SaltWindows][DEBUG][3392]: working_dir=C:\Watchmaker\WorkingFiles\Salt-1_5kqmta
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Deleted working directory...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.workers.base.SaltWindows][INFO ][3392]: Exiting cleanup routine...
==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.Client][INFO ][3392]: Reboot scheduled. System will reboot after the script exits.
==> azure-arm: 2019-11-08 17:38:04,648 [watchmaker.Client][INFO ][3392]: Stop time: 2019-11-08 17:38:04.648938
==> azure-arm: Provisioning with Powershell...
==> azure-arm: Provisioning with powershell script: C:\Users\Giann\AppData\Local\Temp\powershell-provisioner582533063
==> azure-arm: Provisioning step had errors: Running the cleanup provisioner, if present...
==> azure-arm:
==> azure-arm: Cleanup requested, deleting resource group ...

As you can see, the Powershell script ends up making the VM reboot, which is not initiated by Packer its self.

==> azure-arm: 2019-11-08 17:38:04,508 [watchmaker.Client][INFO ][3392]: Reboot scheduled. System will reboot after the script exits.

Previous Packer Success for context:

I have successfully built a Windows VM with Packer, installing software via Powershell script provisioner, then follow up with a powershell inline provisioner to sysprep the image.

Thank you for any help on this.

0

There are 0 answers