Have you tried
Install-Module -Name Az -AllowClobber -Force
Import-Module Az -Force



On Monday, June 10, 2019 at 8:37:04 AM UTC-7, Emin Askerov wrote:
>
> 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.   
>
> On Monday, June 10, 2019 at 2:01:54 PM UTC+2, J Metz wrote:
>>
>> 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"
>>  
>> " 
>>
>> On Mon, Jun 10, 2019 at 7:31 AM Emin Askerov <[email protected]> wrote:
>>
>>> 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"
>>>
>>>
>>>
>>>
>>>
>>>
>>> -- 
>>> 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/534a1552-bdc8-4435-9717-8d3aafcf027c%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/packer-tool/534a1552-bdc8-4435-9717-8d3aafcf027c%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>

-- 
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/6bda2de8-c66d-4741-8b1d-78c7a362e4ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to