hashicorp/packer · error
Error building powershell wrapper: %w
Error message
Error building powershell wrapper: %w
What it means
After assembling context data (Vars, Payload, DebugMode, generated data), extractInlineScript renders the wrapPowershellString template with interpolate.Render. If template rendering fails (bad interpolation syntax, missing/invalid variable types, invalid functions), it throws "Error building powershell wrapper: %w".
Source
Thrown at provisioner/powershell/provisioner.go:330
// we concatenate all the inline commands
for _, command := range p.config.Inline {
log.Printf("Found command: %s", command)
if _, err := commandBuilder.WriteString(command + "\n\t"); err != nil {
return "", fmt.Errorf("failed to wrap script contents: %w", err)
}
}
// injecting all the variables in the string
ctxData := p.generatedData
ctxData["Vars"] = p.createFlattenedEnvVars(p.config.ElevatedUser != "")
ctxData["Payload"] = commandBuilder.String()
ctxData["DebugMode"] = p.config.DebugMode
p.config.ctx.Data = ctxData
data, err := interpolate.Render(wrapPowershellString, &p.config.ctx)
if err != nil {
return "", fmt.Errorf("Error building powershell wrapper: %w", err)
}
log.Printf("Writing PowerShell script to file: %s", temp.Name())
if _, err := temp.WriteString(data); err != nil {
return "", fmt.Errorf("Error writing PowerShell script: %w", err)
}
return temp.Name(), nil
}
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
ui.Say("Provisioning with Powershell...")
p.communicator = comm
p.generatedData = generatedData
scripts := make([]string, len(p.config.Scripts))
copy(scripts, p.config.Scripts)
View on GitHub (pinned to eb36e3c3e4)
Solutions
- Inspect the wrapped cause and fix the interpolation expression in the inline script.
- Escape literal Go-template delimiters in PowerShell (use {{`{{`}} or avoid `{{` sequences in scripts).
- Update references to generated data keys to match your Packer version's documented set.
- Test the inline script with a minimal template to isolate the failing expression.
Example fix
// before (inline command with literal braces)
"Write-Output "{{ "it" }}""
// after (escape braces or remove template syntax)
"Write-Output '{{ it }}'" Defensive patterns
Strategy: try-catch
Validate before calling
// Before adding inline commands, scan for suspicious Go-template syntax
for _, c := range cfg.Inline {
if strings.Contains(c, "{{") && !strings.Contains(c, "{{ `") {
log.Printf("inline command uses interpolation: %q — verify referenced keys exist", c)
}
} Try / catch
// Go
err := prov.Provision(ctx, ui, comm, genData)
if err != nil {
var re *interpolate.RenderError // or inspect wrapped cause
if strings.Contains(err.Error(), "Error building powershell wrapper") {
fmt.Fprintf(os.Stderr, "template interpolation failed: %v\n", err)
// fix inline template expression or escape {{ }} and retry
}
return err
} Prevention
- Escape literal Go-template braces in PowerShell code with backtick-delimited strings ({{`{{`}}).
- Only use documented build/generated data keys in inline scripts.
- Test inline templates with `packer console` or a minimal build first.
- Pin Packer version and read generated-data docs for that version.
When it happens
Trigger: extractInlineScript called from Provision when the Go text/template wrapPowershellString fails against the ctx data — e.g. a generated-data key used in an incompatible way or an interpolation function error.
Common situations: Using template variables in inline scripts that reference generated data unavailable at render time; a malformed {{ }} expression inside an inline command; version changes to generated data keys (e.g. build-specific variables).
Related errors
- failed to wrap script contents: %w
- Error processing command: %s
- Only one of script or scripts can be specified.
- Must supply an 'elevated_user' if 'elevated_password' provid
- Either a script file or inline script must be specified.
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/19b22cd69a785d26.
Report an issue: GitHub.