Skip to content

Conversation

@MBradbury
Copy link
Contributor

Description

When running the Powershell provisioner with inline commands, those inline commands may not set LASTEXITCODE. However, the provisioner will always check the value of LASTEXITCODE (https://github.com/hashicorp/packer/blob/main/provisioner/powershell/provisioner.go#L41).

provisioner "powershell" {
        elevated_user     = var.winrm_username
        elevated_password = var.winrm_password
        inline = [
            "Set-StrictMode -Version Latest",
            "$ProgressPreference = 'SilentlyContinue'",
            "Write-Host \"This is a message that does not set LASTEXITCODE\""
        ]
    }

Will produce output such as the following:

2025-11-26T10:20:41Z: ==> qemu.vm: The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
2025-11-26T10:20:41Z: ==> qemu.vm: At C:\Windows\Temp\script-6926d46b-b194-43a8-7e18-73aa50bc1bdd.ps1:22 char:6
2025-11-26T10:20:41Z: ==> qemu.vm: +     if ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) {
2025-11-26T10:20:41Z: ==> qemu.vm: +         ~~~~~~~~~~~~~
2025-11-26T10:20:41Z: ==> qemu.vm:     + CategoryInfo          : InvalidOperation: (LASTEXITCODE:String) [], RuntimeException
2025-11-26T10:20:41Z: ==> qemu.vm:     + FullyQualifiedErrorId : VariableIsUndefined
2025-11-26T10:20:41Z: ==> qemu.vm:

This PR checks if LASTEXITCODE has been set before checking its value.

Rollback Plan

If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

No changes to security controls.

@MBradbury MBradbury requested a review from a team as a code owner November 26, 2025 12:12
@hashicorp-cla-app
Copy link

hashicorp-cla-app bot commented Nov 26, 2025

CLA assistant check
All committers have signed the CLA.

@hashicorp-cla-app
Copy link

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes

Have you signed the CLA already but the status is still pending? Recheck it.

@kp2099 kp2099 self-assigned this Dec 10, 2025
@kp2099
Copy link
Collaborator

kp2099 commented Dec 10, 2025

Hi @MBradbury Thank you for your contribution. This change makes sense to me. please fix the test cases as well.

@kp2099 kp2099 merged commit 74cc342 into hashicorp:main Jan 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants