I’m trying to generate an AWS AMI with Packer. EC2 Instance and WinRM are setup correctly but when I execute a “”“simple”“” PowerShell script:
echo "Demo"
Write-Host "Demo"
Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe
if ($? == $False) {
Exit 11
}
If keep only echo and Write-Host it exits successfully and with output in Packer logs.
I runned Packer with environment variable PACKER_LOG=1 and it prints:
2025/07/16 16:37:16 packer-provisioner-powershell plugin: Opening ./demo.ps1 for reading
2025/07/16 16:37:16 packer-provisioner-powershell plugin: Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-6877b91c-2556-fc69-578c-8006203e88b3.ps1
2025/07/16 16:37:16 packer-provisioner-powershell plugin: [INFO] 82 bytes written for 'uploadData'
2025/07/16 16:37:16 [INFO] 82 bytes written for 'uploadData'
2025/07/16 16:37:17 packer-plugin-amazon_v1.3.7_x5.0_linux_amd64 plugin: 2025/07/16 16:37:16 Uploading file to 'c:/Windows/Temp/packer-ps-env-vars-6877b91c-2556-fc69-578c-8006203e88b3.ps1'
2025/07/16 16:37:24 packer-provisioner-powershell plugin: [INFO] 914 bytes written for 'uploadData'
2025/07/16 16:37:24 [INFO] 914 bytes written for 'uploadData'
2025/07/16 16:37:24 packer-plugin-amazon_v1.3.7_x5.0_linux_amd64 plugin: 2025/07/16 16:37:24 Uploading file to 'c:/Windows/Temp/script-6877b91c-c954-9e79-a502-9c66c87a1c76.ps1'
2025/07/16 16:37:32 packer-plugin-amazon_v1.3.7_x5.0_linux_amd64 plugin: 2025/07/16 16:37:32 [INFO] starting remote command: powershell -executionpolicy bypass -file c:/Windows/Temp/script-6877b91c-c954-9e79-a502-9c66c87a1c76.ps1
2025/07/16 16:37:51 packer-plugin-amazon_v1.3.7_x5.0_linux_amd64 plugin: 2025/07/16 16:37:51 [INFO] command 'powershell -executionpolicy bypass -file c:/Windows/Temp/script-6877b91c-c954-9e79-a502-9c66c87a1c76.ps1' exited with code: 2
2025/07/16 16:37:51 packer-plugin-amazon_v1.3.7_x5.0_linux_amd64 plugin: 2025/07/16 16:37:51 [INFO] RPC endpoint: Communicator ended with: 2
2025/07/16 16:37:51 [INFO] 0 bytes written for 'stdout'
2025/07/16 16:37:51 [INFO] 0 bytes written for 'stderr'
2025/07/16 16:37:51 [INFO] RPC client: Communicator ended with: 2
2025/07/16 16:37:51 [INFO] RPC endpoint: Communicator ended with: 2
2025/07/16 16:37:51 packer-provisioner-powershell plugin: [INFO] 0 bytes written for 'stdout'
2025/07/16 16:37:51 packer-provisioner-powershell plugin: [INFO] 0 bytes written for 'stderr'
2025/07/16 16:37:51 packer-provisioner-powershell plugin: [INFO] RPC client: Communicator ended with: 2
2025/07/16 16:37:51 packer-provisioner-powershell plugin: c:/Windows/Temp/script-6877b91c-c954-9e79-a502-9c66c87a1c76.ps1 returned with exit code 2
2025/07/16 16:37:51 [INFO] (telemetry) ending powershell
Any idea on how to debug?