{"record":{"id":"19b22cd69a785d26","repo":"hashicorp/packer","slug":"error-building-powershell-wrapper-w","errorCode":null,"errorMessage":"Error building powershell wrapper: %w","messagePattern":"Error building powershell wrapper: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":330,"sourceCode":"\n\t// we concatenate all the inline commands\n\tfor _, command := range p.config.Inline {\n\t\tlog.Printf(\"Found command: %s\", command)\n\t\tif _, err := commandBuilder.WriteString(command + \"\\n\\t\"); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to wrap script contents: %w\", err)\n\t\t}\n\t}\n\n\t// injecting all the variables in the string\n\tctxData := p.generatedData\n\tctxData[\"Vars\"] = p.createFlattenedEnvVars(p.config.ElevatedUser != \"\")\n\tctxData[\"Payload\"] = commandBuilder.String()\n\tctxData[\"DebugMode\"] = p.config.DebugMode\n\tp.config.ctx.Data = ctxData\n\n\tdata, err := interpolate.Render(wrapPowershellString, &p.config.ctx)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error building powershell wrapper: %w\", err)\n\t}\n\n\tlog.Printf(\"Writing PowerShell script to file: %s\", temp.Name())\n\tif _, err := temp.WriteString(data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error writing PowerShell script: %w\", err)\n\t}\n\n\treturn temp.Name(), nil\n}\n\nfunc (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {\n\tui.Say(\"Provisioning with Powershell...\")\n\tp.communicator = comm\n\tp.generatedData = generatedData\n\n\tscripts := make([]string, len(p.config.Scripts))\n\tcopy(scripts, p.config.Scripts)\n","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L312-L348","documentation":"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\".","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before (inline command with literal braces)\n\"Write-Output \"{{ \"it\" }}\"\"\n// after (escape braces or remove template syntax)\n\"Write-Output '{{ it }}'\"","handlingStrategy":"try-catch","validationCode":"// Before adding inline commands, scan for suspicious Go-template syntax\nfor _, c := range cfg.Inline {\n    if strings.Contains(c, \"{{\") && !strings.Contains(c, \"{{ `\") {\n        log.Printf(\"inline command uses interpolation: %q — verify referenced keys exist\", c)\n    }\n}","typeGuard":null,"tryCatchPattern":"// Go\nerr := prov.Provision(ctx, ui, comm, genData)\nif err != nil {\n    var re *interpolate.RenderError // or inspect wrapped cause\n    if strings.Contains(err.Error(), \"Error building powershell wrapper\") {\n        fmt.Fprintf(os.Stderr, \"template interpolation failed: %v\\n\", err)\n        // fix inline template expression or escape {{ }} and retry\n    }\n    return err\n}","preventionTips":["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."],"tags":["powershell","provisioner","template-interpolation","inline-script"],"backgroundTag":"template-render-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}