Packer hangs during PowerShell inline script execution (Windows 10 image)

2,708 views
Skip to first unread message

Emin Askerov

unread,
Jun 10, 2019, 7:31:27 AM6/10/19
to Packer
Hello All,


I am stuck with simple Packer template which provisions Windows 10 image in Azure. Template includes inline PowerShell script which is executed with elevated privileges and installs Azure PowerShell modules and then generalizes image using sysprep. The issue is that Packer simply hangs during running the inline script.  I’ve tried to debug this script on the separate VM and it works as expected, but when it is executed within Packer template it hangs for some reason. Template and debug logs are provided below:


Any ideas what am I doing wrong or this is a bug?


{
    "variables": {
        "azure_region": "West Europe",
        "azure_image_publisher": "MicrosoftWindowsDesktop",
        "azure_image_offer": "Windows-10",
        "azure_image_sku": "rs5-pron",
        "azure_resource_group": "Test-RG",
        "azure_vm_size": "Standard_D2s_v3"
      },
    "builders": [{
      "type": "azure-arm",
  
      "client_id": "{{user `azure_client_id`}}",
      "client_secret": "{{user `azure_client_secret`}}",
      "tenant_id": "{{user `azure_tenant_id`}}",
      "subscription_id": "{{user `azure_subscription_id`}}",
  
      "managed_image_resource_group_name": "{{user `azure_resource_group`}}",
      "managed_image_name": "{{user `azure_image_offer` | lower}}-{{user `azure_image_sku` | lower}}-{{isotime \"02Jan2006_1504\"}}",
  
      "os_type": "Windows",
      "image_publisher": "{{user `azure_image_publisher`}}",
      "image_offer": "{{user `azure_image_offer`}}",
      "image_sku": "{{user `azure_image_sku`}}",
  
      "communicator": "winrm",
      "winrm_use_ssl": true,
      "winrm_insecure": true,
      "winrm_timeout": "5m",
      "winrm_username": "packer",
  
      "location": "{{user `azure_region`}}",
      "vm_size": "{{user `azure_vm_size`}}"
    }],
    "provisioners": [{
      "type": "powershell",
      "elevated_user": "packer",
      "elevated_password": "{{.WinRMPassword}}",
      "inline": [
        "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine -Force",
        "Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force",
        "Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted",
        "Install-Module -Name Az",
        "Import-Module Az",
        "& $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 } }"
      ]
    }]
  }


Debug log:


==> azure-arm: Provisioning with Powershell...
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine -Force
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: Install-Module -Name Az
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: Import-Module Az
==> azure-arm: Provisioning with powershell script: C:\Users\easkerov\AppData\Local\Temp\powershell-provisioner337119215
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: & $env:SystemRoot\System32\Sysprep\Sysprep.exe /oobe /generalize /quiet /quit
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Found command: 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 } }
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Opening C:\Users\easkerov\AppData\Local\Temp\powershell-provisioner337119215 for reading
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 [INFO] 74 bytes written for 'uploadData'
2019/06/10 13:04:14 [INFO] 74 bytes written for 'uploadData'
2019/06/10 13:04:14 packer.exe: 2019/06/10 13:04:14 Uploading file to 'c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1'
2019/06/10 13:04:15 packer.exe: #< CLIXML
2019/06/10 13:04:16 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>#< CLIXML
2019/06/10 13:04:16 packer.exe: 2019/06/10 13:04:16 Building elevated command wrapper for: powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1; &'c:/Windows/Temp/script-5cfe3672-4f87-5b9e-285e-e0dcc8d92df4.ps1'; exit $LastExitCode }"
2019/06/10 13:04:16 packer.exe: 2019/06/10 13:04:16 Command [powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1; &'c:/Windows/Temp/script-5cfe3672-4f87-5b9e-285e-e0dcc8d92df4.ps1'; exit $LastExitCode }" > %SYSTEMROOT%/Temp/packer-5cfe3930-81a9-eac0-692e-aa06bc696b22.out 2>&1] converted to [powershell -executionpolicy bypass &#34;&amp; { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value &#39;SilentlyContinue&#39;};. c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1; &amp;&#39;c:/Windows/Temp/script-5cfe3672-4f87-5b9e-285e-e0dcc8d92df4.ps1&#39;; exit $LastExitCode }&#34; &gt; %SYSTEMROOT%/Temp/packer-5cfe3930-81a9-eac0-692e-aa06bc696b22.out 2&gt;&amp;1] for use in XML string
2019/06/10 13:04:16 packer.exe: 2019/06/10 13:04:16 Uploading elevated shell wrapper for command [powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-5cfe3672-7c28-9aa9-7377-69c61ab76e2f.ps1; &'c:/Windows/Temp/script-5cfe3672-4f87-5b9e-285e-e0dcc8d92df4.ps1'; exit $LastExitCode }" > %SYSTEMROOT%/Temp/packer-5cfe3930-81a9-eac0-692e-aa06bc696b22.out 2>&1] to [C:/Windows/Temp/packer-elevated-shell-5cfe3930-983a-f36e-80b0-4053f8326643.ps1]
2019/06/10 13:04:16 packer.exe: 2019/06/10 13:04:16 [INFO] 3230 bytes written for 'uploadData'
2019/06/10 13:04:16 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>2019/06/10 13:04:16 Uploading file to 'C:/Windows/Temp/packer-elevated-shell-5cfe3930-983a-f36e-80b0-4053f8326643.ps1'
2019/06/10 13:04:16 [INFO] 3230 bytes written for 'uploadData'
2019/06/10 13:04:18 packer.exe: #< CLIXML
2019/06/10 13:04:19 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>#< CLIXML
2019/06/10 13:04:19 packer.exe: 2019/06/10 13:04:19 [INFO] 603 bytes written for 'uploadData'
2019/06/10 13:04:19 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>2019/06/10 13:04:19 Uploading file to 'c:/Windows/Temp/script-5cfe3672-4f87-5b9e-285e-e0dcc8d92df4.ps1'
2019/06/10 13:04:19 [INFO] 603 bytes written for 'uploadData'
2019/06/10 13:04:20 packer.exe: #< CLIXML
2019/06/10 13:04:21 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>#< CLIXML
2019/06/10 13:04:21 packer.exe: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>2019/06/10 13:04:21 [INFO] starting remote command: powershell -executionpolicy bypass -file "C:/Windows/Temp/packer-elevated-shell-5cfe3930-983a-f36e-80b0-4053f8326643.ps1"






J Metz

unread,
Jun 10, 2019, 8:01:54 AM6/10/19
to packe...@googlegroups.com
Put an overarching "Try\Catch" statement around the inline script and set each of the cmdlets "ErrorAction" property to "Stop".  In the Catch block do a Write-Output such as the one below.  This will echo out any errors you encounter..

Write-Output "This is the error...  Error Details: $($_.Exception.Message)"

Also, make sure you have everything in the inline script quoted and escaped correctly.  You can verify this by taking a look a the resultant scripts Packer creates on the host machine.  In your case, per your last log, the file path to check is "C:/Windows/Temp/packer-elevated-shell-5cfe3930-983a-f36e-80b0-4053f8326643.ps1"
"

--
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 packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/534a1552-bdc8-4435-9717-8d3aafcf027c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emin Askerov

unread,
Jun 10, 2019, 11:37:04 AM6/10/19
to Packer
Hello,

Thank you for reply. 

I've double checked the script. It works correctly, without any user prompts. 
Also I've tried to replaced inline commands with external script 

"provisioners": [{
"type": "powershell",
"remote_path": "C:/Windows/Temp/pwshscript.ps1",
"elevated_user": "packer",
"elevated_password": "{{.WinRMPassword}}",
"script" : "pwshscript.ps1"
}]

However issue remains the same. Packer  image provisioning process hangs with logs I've provided earlier.

I am stuck to be honest...

Thanks.   
To unsubscribe from this group and stop receiving emails from it, send an email to packe...@googlegroups.com.

Saeid Rezaei Baghbidi

unread,
Jul 10, 2019, 9:43:29 PM7/10/19
to Packer
Have you tried
Install-Module -Name Az -AllowClobber -Force
Import-Module Az -Force

Sim

unread,
Jul 29, 2019, 8:34:39 PM7/29/19
to Packer
Hi, 

I have experience the same issue, the PS script is just hanging there when I run it with elevated PS. 

{
       "type": "powershell",
       "elevated_user": "packer",
       "elevated_password": "{{.WinRMPassword}}",
       "inline": [
         "Install-Module DockerMsftProvider -Force",
         "Install-Package Docker -ProviderName DockerMsftProvider -Force"
       ]
     }

It's just hanging like this:

Capture.PNG

Is there any solution for this?
Thanks 

Jeremiah Snapp

unread,
May 19, 2020, 5:11:09 PM5/19/20
to Packer
I was having the same trouble. I was trying to run an `install.ps1` script using the elevated_user and elevated_password. This worked fine on Windows Server 2012/2016/2019 in AWS but failed to run on Windows 10 in Azure. The provisioner would just hang indefinitely.

    {
     "type": "powershell",
     "elevated_user": "packer",
     "elevated_password": "{{.WinRMPassword}}",
     "script": "scripts/install.ps1"
   }

I did a lot of digging into the problem and discovered that elevated_user and elevated_password tells packer to create a Windows scheduled task to run the provisioner's script (or inline commands). For some reason Windows 10 won't run the scheduled task until the admin user (packer in this case) has logged into the system. I spent a lot of time trying to figure out what it was about logging into the system that allowed the scheduled task to run but I couldn't figure it out. I got things working by enabling auto-logon for the packer user and restarting the system prior to running the provisioner that uses elevated_user. After the scheduled task runs I disable auto-logon.

    {
     "type": "powershell",
     "script": "scripts/enable-autologon.ps1",
     "environment_vars": ["ADMIN_PASSWORD={{.WinRMPassword}}"]
   },
    {
     "type": "windows-restart"
   },
   {
     "type": "powershell",
     "elevated_user": "packer",
     "elevated_password": "{{.WinRMPassword}}",
     "script": "scripts/install.ps1"
   },
   {
     "type": "powershell",
     "script": "scripts/disable-autologon.ps1"
   }


Here are the contents of enable-autologon.ps1
# Stop script execution when a non-terminating error occurs
$ErrorActionPreference = "Stop"

If ([string]::IsNullOrEmpty($Env:ADMIN_PASSWORD)) { Throw "Env:ADMIN_PASSWORD must be set" }

# Our testing has shown that Windows 10 does not allow packer to run a Windows scheduled task until the admin user (packer) has logged into the system.
# So we enable AutoAdminLogon and use packer's windows-restart provisioner to get the system into a good state to allow scheduled tasks to run.
Write-Output "Enabling AutoAdminLogon to allow packer's scheduled task created by elevated_user to run..."
Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name AutoAdminLogon -Value 1 -type String
Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name DefaultUsername -Value $Env:UserName -type String
Set-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name DefaultPassword -Value "$Env:ADMIN_PASSWORD" -type String


Here are the contents of disable-autologon.ps1
# Stop script execution when a non-terminating error occurs
$ErrorActionPreference = "Stop"

Write-Output "Disabling AutoAdminLogon..."
Remove-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name AutoAdminLogon
Remove-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name DefaultUsername
Remove-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -Name DefaultPassword



Reply all
Reply to author
Forward
0 new messages