hashicorp/packer · error

Error uploading ps script containing env vars: %s

Error message

Error uploading ps script containing env vars: %s

What it means

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.

Source

Thrown at provisioner/powershell/provisioner.go:576

	// Re-assemble vars using OS specific format pattern and flatten
	for _, key := range keys {
		flattened += fmt.Sprintf(format, key, envVars[key])
	}
	return
}

func (p *Provisioner) uploadEnvVars(flattenedEnvVars string) (err error) {
	ctx := context.TODO()
	// Upload all env vars to a powershell script on the target build file
	// system. Do this in the context of a single retryable function so that
	// we gracefully handle any errors created by transient conditions such as
	// a system restart
	envVarReader := strings.NewReader(flattenedEnvVars)
	log.Printf("Uploading env vars to %s", p.config.RemoteEnvVarPath)
	err = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(context.Context) error {
		if err := p.communicator.Upload(p.config.RemoteEnvVarPath, envVarReader, nil); err != nil {
			return fmt.Errorf("Error uploading ps script containing env vars: %s", err)
		}
		return err
	})
	return
}

func (p *Provisioner) createCommandText() (command string, err error) {
	// Return the interpolated command
	if p.config.ElevatedUser == "" {
		return p.createCommandTextNonPrivileged()
	} else {
		return p.createCommandTextPrivileged()
	}
}

func (p *Provisioner) createCommandTextNonPrivileged() (command string, err error) {
	// Prepare everything needed to enable the required env vars within the
	// remote environment

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Inspect the wrapped cause in the message (after the colon) to see the transport-level reason.
  2. Ensure the guest stays up and connected for the whole provision phase; disable guest auto-restart during builds.
  3. Verify the env var script path's directory is writable by the communicator user.
  4. Increase start_retry_timeout to ride out longer transient outages.
  5. Retry the build; transient conditions are explicitly expected to be handled by the retry loop.

Example fix

// before
"start_retry_timeout": "5m"
// after
"start_retry_timeout": "10m"
Defensive patterns

Strategy: retry

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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