hashicorp/packer · error

Error writing PowerShell script: %w

Error message

Error writing PowerShell script: %w

What it means

After rendering the wrapper script, extractInlineScript writes the rendered text to a temporary file via temp.WriteString. If the write fails, it throws "Error writing PowerShell script: %w" and Provision aborts before upload.

Source

Thrown at provisioner/powershell/provisioner.go:335

			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)

	if p.config.Inline != nil {
		temp, err := extractInlineScript(p)
		if err != nil {
			ui.Error(fmt.Sprintf("Unable to extract inline scripts into a file: %s", err))
		}

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Free disk space on the volume backing the OS temp directory.
  2. Point TMPDIR (or %TEMP%) at a writable location with enough space.
  3. Exclude the Packer temp files from antivirus/EDR real-time locking.
  4. Re-run the build; transient locking often resolves on retry.

Example fix

// shell
// before: TMPDIR unset, /tmp full
// after
export TMPDIR=/var/tmp && packer build template.pkr.hcl
Defensive patterns

Strategy: validation

Validate before calling

// Go, before Provision: ensure temp volume is writable with headroom
if st, err := os.Stat(os.TempDir()); err != nil || !st.IsDir() {
    return fmt.Errorf("temp dir %q unusable: %v", os.TempDir(), err)
}
// Optionally check free space via syscall/statfs before long builds.

Try / catch

// Go
err := prov.Provision(ctx, ui, comm, genData)
if err != nil && strings.Contains(err.Error(), "Error writing PowerShell script") {
    // check disk space / AV locks on %TEMP% or $TMPDIR, then retry
}

Prevention

When it happens

Trigger: extractInlineScript called from Provision when writing the rendered wrapper to the temp file fails — disk full, temp directory removed mid-run, permission loss on the temp file, or the temp file already closed.

Common situations: Full /tmp or %TEMP% disk; restrictive TMPDIR; antivirus locking the temp file; container images with read-only tmpfs.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/277d17c4036ba7b9. Report an issue: GitHub.