{"record":{"id":"532215257224921e","repo":"hashicorp/packer","slug":"error-stating-powershell-script-s","errorCode":null,"errorMessage":"Error stating powershell script: %s","messagePattern":"Error stating powershell script: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":367,"sourceCode":"\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}\n\t\tscripts = append(scripts, temp)\n\t\t// Remove temp script containing the inline commands when done\n\t\tdefer os.Remove(temp)\n\t}\n\n\t// every provisioner run will only have one env var script file so lets add it first\n\tuploadedScripts := []string{p.config.RemoteEnvVarPath}\n\tfor _, path := range scripts {\n\t\tui.Say(fmt.Sprintf(\"Provisioning with powershell script: %s\", path))\n\n\t\tlog.Printf(\"Opening %s for reading\", path)\n\t\tfi, err := os.Stat(path)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error stating powershell script: %s\", err)\n\t\t}\n\t\tif os.IsPathSeparator(p.config.RemotePath[len(p.config.RemotePath)-1]) {\n\t\t\t// path is a directory\n\t\t\tp.config.RemotePath += filepath.Base(fi.Name())\n\t\t}\n\t\tf, err := os.Open(path)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error opening powershell script: %s\", err)\n\t\t}\n\t\tdefer f.Close()\n\n\t\tcommand, err := p.createCommandText()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error processing command: %s\", err)\n\t\t}\n\n\t\t// Upload the file and run the command. Do this in the context of a\n\t\t// single retryable function so that we don't end up with the case","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L349-L385","documentation":"In Provision, before uploading, each script path is re-verified with os.Stat. If Stat fails at run time (the file disappeared after Prepare, or the script is generated and its path invalid), Provision returns \"Error stating powershell script: %s\" and stops provisioning.","triggerScenarios":"Provision called with a script path in p.config.Scripts (or the path returned by extractInlineScript) that no longer exists or cannot be stat-ed at build time.","commonSituations":"Script generated into a temp dir that a previous step cleaned; scripts array entries pointing at files deleted between validate and build; running packer on a different machine/container than where scripts were created; cwd changed so relative paths break at runtime.","solutions":["Ensure every `scripts` path exists at build time on the host running packer; use absolute paths.","If the script is created by an earlier provisioner/step, guarantee it is written before the powershell provisioner runs.","Re-run `packer validate` and check the resolved paths in debug output (log shows 'Opening %s for reading').","Use `inline` scripts when the content is dynamic instead of pre-generated temp files."],"exampleFix":"// before\n\"scripts\": [\"/tmp/generated.ps1\"]  // temp dir cleaned earlier\n// after: write to a stable project path\n\"scripts\": [\"{{pwd}}/generated/bootstrap.ps1\"]","handlingStrategy":"validation","validationCode":"// Go, immediately before Provision\nfor _, p := range cfg.Scripts {\n    if _, err := os.Stat(p); err != nil {\n        return fmt.Errorf(\"script %q missing at provision time: %w\", p, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"// Go\nerr := prov.Provision(ctx, ui, comm, genData)\nif err != nil && strings.Contains(err.Error(), \"Error stating powershell script\") {\n    // regenerate or re-locate the script, then retry Provision\n}","preventionTips":["Use absolute paths so cwd changes between validate and build don't break resolution.","Don't write scripts to shared temp dirs that other steps clean.","Order provisioners so any script-generating step runs before the powershell provisioner.","Verify scripts exist on the same host/container that runs packer, not a remote builder."],"tags":["powershell","provisioner","file-not-found","runtime"],"backgroundTag":"script-file-not-found","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"}