{"record":{"id":"47e9368717e75552","repo":"hashicorp/packer","slug":"error-uploading-ps-script-containing-env-vars-s","errorCode":null,"errorMessage":"Error uploading ps script containing env vars: %s","messagePattern":"Error uploading ps script containing env vars: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":576,"sourceCode":"\n\t// Re-assemble vars using OS specific format pattern and flatten\n\tfor _, key := range keys {\n\t\tflattened += fmt.Sprintf(format, key, envVars[key])\n\t}\n\treturn\n}\n\nfunc (p *Provisioner) uploadEnvVars(flattenedEnvVars string) (err error) {\n\tctx := context.TODO()\n\t// Upload all env vars to a powershell script on the target build file\n\t// system. Do this in the context of a single retryable function so that\n\t// we gracefully handle any errors created by transient conditions such as\n\t// a system restart\n\tenvVarReader := strings.NewReader(flattenedEnvVars)\n\tlog.Printf(\"Uploading env vars to %s\", p.config.RemoteEnvVarPath)\n\terr = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(context.Context) error {\n\t\tif err := p.communicator.Upload(p.config.RemoteEnvVarPath, envVarReader, nil); err != nil {\n\t\t\treturn fmt.Errorf(\"Error uploading ps script containing env vars: %s\", err)\n\t\t}\n\t\treturn err\n\t})\n\treturn\n}\n\nfunc (p *Provisioner) createCommandText() (command string, err error) {\n\t// Return the interpolated command\n\tif p.config.ElevatedUser == \"\" {\n\t\treturn p.createCommandTextNonPrivileged()\n\t} else {\n\t\treturn p.createCommandTextPrivileged()\n\t}\n}\n\nfunc (p *Provisioner) createCommandTextNonPrivileged() (command string, err error) {\n\t// Prepare everything needed to enable the required env vars within the\n\t// remote environment","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L558-L594","documentation":"The PowerShell provisioner flattens the configured environment variables into a .ps1 file and uploads it to RemoteEnvVarPath inside a retry loop (StartRetryTimeout). This error wraps any failure from that communicator Upload. The env-vars script never landed on the guest, so command execution that depends on it will fail or run without those variables.","triggerScenarios":"communicator.Upload of the env-var .ps1 fails: transient SSH/WinRM disconnects across all retries, permission denied at RemoteEnvVarPath, guest disk full, or upload protocol errors.","commonSituations":"Guest reboots or loses network during provisioning; env_var_script_path's directory not writable; antivirus on Windows guests locking newly created files in temp; long builds where the session times out.","solutions":["Inspect the wrapped cause in the message (after the colon) to see the transport-level reason.","Ensure the guest stays up and connected for the whole provision phase; disable guest auto-restart during builds.","Verify the env var script path's directory is writable by the communicator user.","Increase start_retry_timeout to ride out longer transient outages.","Retry the build; transient conditions are explicitly expected to be handled by the retry loop."],"exampleFix":"// before\n\"start_retry_timeout\": \"5m\"\n// after\n\"start_retry_timeout\": \"10m\"","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Read the wrapped cause after the colon to identify the transport failure.","Increase start_retry_timeout for guests prone to restarts.","Keep env var script path in a writable directory.","Stabilize guest network/SSH or WinRM service during builds."],"tags":["network","upload","powershell","environment-variables"],"backgroundTag":"communicator-upload-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"}