{"record":{"id":"4b7d106450754803","repo":"hashicorp/packer","slug":"environment-variable-not-in-format-key-value-s","errorCode":null,"errorMessage":"Environment variable not in format 'key=value': %s","messagePattern":"Environment variable not in format 'key=value': (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":279,"sourceCode":"\t\t} else {\n\t\t\tp.config.ElevatedExecuteCommand = p.defaultScriptCommand()\n\t\t\tlog.Printf(\"Using script default elevated execute command %s\", p.config.ElevatedExecuteCommand)\n\t\t}\n\t}\n\n\tfor _, path := range p.config.Scripts {\n\t\tif _, err := os.Stat(path); err != nil {\n\t\t\terrs = packersdk.MultiErrorAppend(errs,\n\t\t\t\tfmt.Errorf(\"Bad script '%s': %s\", path, err))\n\t\t}\n\t}\n\n\t// Do a check for bad environment variables, such as '=foo', 'foobar'\n\tfor _, kv := range p.config.Vars {\n\t\tvs := strings.SplitN(kv, \"=\", 2)\n\t\tif len(vs) != 2 || vs[0] == \"\" {\n\t\t\terrs = packersdk.MultiErrorAppend(errs,\n\t\t\t\tfmt.Errorf(\"Environment variable not in format 'key=value': %s\", kv))\n\t\t}\n\t}\n\n\tif p.config.ExecutionPolicy > 7 {\n\t\terrs = packersdk.MultiErrorAppend(errs, fmt.Errorf(`Invalid execution `+\n\t\t\t`policy provided. Please supply one of: \"bypass\", \"allsigned\",`+\n\t\t\t` \"default\", \"remotesigned\", \"restricted\", \"undefined\", `+\n\t\t\t`\"unrestricted\", \"none\".`))\n\t}\n\n\tif !(p.config.DebugMode >= 0 && p.config.DebugMode <= 2) {\n\t\terrs = packersdk.MultiErrorAppend(errs, fmt.Errorf(\"%d is an invalid Trace level for `debug_mode`; valid values are 0, 1, and 2\", p.config.DebugMode))\n\t}\n\n\tif errs != nil {\n\t\treturn errs\n\t}\n","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L261-L297","documentation":"Each entry in the provisioner's `environment_vars` (Vars) must be a single string of the form 'key=value'. Prepare splits each entry on '=' with strings.SplitN; if there is no '=' or the key is empty, it throws \"Environment variable not in format 'key=value': %s\". This keeps PowerShell env var injection well-formed.","triggerScenarios":"Prepare called with config.Vars entries like \"foo\" (no '='), \"=bar\" (empty key), or an entry where the value was accidentally omitted.","commonSituations":"Writing env vars as a JSON object instead of an array of key=value strings; forgetting the value; leading '=' typos; quoting mistakes in HCL2 that drop the '='.","solutions":["Rewrite each entry as a single 'KEY=value' string inside the vars array.","Quote the entry so an embedded '=' isn't split away: vars = [\"MYVAR=with=equals\"] works, vars = [\"MYVAR\"] does not.","If the value is empty, still include the '=': \"MYVAR=\"."],"exampleFix":"// before (HCL)\nenvironment_vars = [\"MYVAR\"]\n// after\nenvironment_vars = [\"MYVAR=somevalue\"]","handlingStrategy":"validation","validationCode":"// Go, before Prepare\nfor _, kv := range cfg.Vars {\n    parts := strings.SplitN(kv, \"=\", 2)\n    if len(parts) != 2 || parts[0] == \"\" {\n        return fmt.Errorf(\"env var %q must be KEY=value\", kv)\n    }\n}","typeGuard":null,"tryCatchPattern":"// Go\nif err := prov.Prepare(cfg); err != nil {\n    if strings.Contains(err.Error(), \"Environment variable not in format\") {\n        // normalize cfg.Vars entries to KEY=value and retry Prepare\n    }\n    return err\n}","preventionTips":["Always express environment_vars as an array of \"KEY=value\" strings, never bare keys.","Include '=' even when the value is empty (\"KEY=\").","Quote values containing '='; SplitN only splits on the first '='.","Add a CI check that scans template files for malformed var entries."],"tags":["powershell","provisioner","environment-variables","configuration"],"backgroundTag":"invalid-env-var-format","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"}