Setting an environment variable in javascript

How to set environment variable in jscript WSH file that calls another program? Here is a test case:

envtest.js
----------
var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST_ENV_VAR") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);    

envtest.bat
-----------
set
pause

I expect to see TEST_ ENV _VAR in the variable list, but it is not. What's wrong?

edit:

If someone can create a sample working code, I will mark it as the correct answer. :)

+5
source share
3 answers

The problem is not your code, but the execution process. Complete system variables are assigned to the process that is running. therefore, your child process also had the same set of variables.

Your sample code works well. It adds a variable to the SYSTEM environment.

, , .

.

var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST1") = "TEST_VALUE";
var oSysEnv = oShell.Environment("PROCESS");
oSysEnv("TEST1") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);  

.

. , "SET".

.

+6

4 "" (System, User, Volatile Process), , , Process, ,

+2

. , ; .

Also I don't know if the argument is case Environment()sensitive or not. The MS documentation is "SYSTEM"used instead "SYSTEM". Could matter, but I don’t know for sure.

0
source

All Articles