{"record":{"id":"277d17c4036ba7b9","repo":"hashicorp/packer","slug":"error-writing-powershell-script-w","errorCode":null,"errorMessage":"Error writing PowerShell script: %w","messagePattern":"Error writing PowerShell script: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":335,"sourceCode":"\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\n\tif p.config.Inline != nil {\n\t\ttemp, err := extractInlineScript(p)\n\t\tif err != nil {\n\t\t\tui.Error(fmt.Sprintf(\"Unable to extract inline scripts into a file: %s\", err))\n\t\t}","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L317-L353","documentation":"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.","triggerScenarios":"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.","commonSituations":"Full /tmp or %TEMP% disk; restrictive TMPDIR; antivirus locking the temp file; container images with read-only tmpfs.","solutions":["Free disk space on the volume backing the OS temp directory.","Point TMPDIR (or %TEMP%) at a writable location with enough space.","Exclude the Packer temp files from antivirus/EDR real-time locking.","Re-run the build; transient locking often resolves on retry."],"exampleFix":"// shell\n// before: TMPDIR unset, /tmp full\n// after\nexport TMPDIR=/var/tmp && packer build template.pkr.hcl","handlingStrategy":"validation","validationCode":"// Go, before Provision: ensure temp volume is writable with headroom\nif st, err := os.Stat(os.TempDir()); err != nil || !st.IsDir() {\n    return fmt.Errorf(\"temp dir %q unusable: %v\", os.TempDir(), err)\n}\n// Optionally check free space via syscall/statfs before long builds.","typeGuard":null,"tryCatchPattern":"// Go\nerr := prov.Provision(ctx, ui, comm, genData)\nif err != nil && strings.Contains(err.Error(), \"Error writing PowerShell script\") {\n    // check disk space / AV locks on %TEMP% or $TMPDIR, then retry\n}","preventionTips":["Keep several hundred MB free on the volume backing the temp directory.","Set TMPDIR/%TEMP% to a known-writable path in CI containers.","Exclude packer temp files from antivirus real-time scanning.","Avoid read-only tmpfs mounts for build workloads."],"tags":["powershell","provisioner","filesystem","temp-file"],"backgroundTag":"temp-file-write-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"}