在javascript中设置环境变量

时间:2022-03-31 01:45:12

How can I set an environment variable in WSH jscript file that calls another program? Here's the reduced test case:

如何在WSH jscript文件中设置一个调用另一个程序的环境变量?这是减少的测试用例:

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 list of variables, but it's not there. What's wrong?

我期望在变量列表中看到TEST_ ENV _VAR,但它不存在。怎么了?

edit:

If someone can produce a working code sample, I'll mark that as the correct answer. :)

如果有人可以生成有效的代码示例,我会将其标记为正确的答案。 :)

3 个解决方案

#1


The problem is not in your code, but it is in execution of the process. The complete system variables are assigned to the process which is executed. so, your child process also had the same set of variables.

问题不在您的代码中,而是在执行过程中。将完整的系统变量分配给执行的过程。所以,你的子进程也有相同的变量集。

Your code-sample works good. It adds the variable to the SYSTEM environment.

您的代码示例运行良好。它将变量添加到SYSTEM环境中。

So, you need to set the variable not only for your system but also for your process.

因此,您不仅需要为系统设置变量,还需要为过程设置变量。

Here's the code.

这是代码。

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);  

Once you created the system variable.

一旦创建了系统变量。

It will assign the newly created variable for the current process. So, your child process can get that variable while the "SET" command executed.

它将为当前进程分配新创建的变量。因此,您的子进程可以在执行“SET”命令时获取该变量。

Sorry for my bad-english.

对不起,我的英语不好。

#2


There are 4 "collections" (System, User, Volatile, and Process) you probably want Process if you just need a child process to see the variable

如果您只需要子进程来查看变量,那么您可能需要4个“集合”(系统,用户,易失性和进程)进程

#3


You are getting the system environment variables. I suspect you simply don't have permission to modify them; you could try changing this to the user environment variables.

您正在获取系统环境变量。我怀疑你根本没有权限修改它们;您可以尝试将其更改为用户环境变量。

Also I don't know whether the argument to Environment() is case-sensitive or not. MS's documentation uses "System" instead of "SYSTEM". Might make a difference but I don't know for sure.

另外我不知道Environment()的参数是否区分大小写。 MS的文档使用“系统”而不是“系统”。可能会有所作为,但我不确定。

#1


The problem is not in your code, but it is in execution of the process. The complete system variables are assigned to the process which is executed. so, your child process also had the same set of variables.

问题不在您的代码中,而是在执行过程中。将完整的系统变量分配给执行的过程。所以,你的子进程也有相同的变量集。

Your code-sample works good. It adds the variable to the SYSTEM environment.

您的代码示例运行良好。它将变量添加到SYSTEM环境中。

So, you need to set the variable not only for your system but also for your process.

因此,您不仅需要为系统设置变量,还需要为过程设置变量。

Here's the code.

这是代码。

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);  

Once you created the system variable.

一旦创建了系统变量。

It will assign the newly created variable for the current process. So, your child process can get that variable while the "SET" command executed.

它将为当前进程分配新创建的变量。因此,您的子进程可以在执行“SET”命令时获取该变量。

Sorry for my bad-english.

对不起,我的英语不好。

#2


There are 4 "collections" (System, User, Volatile, and Process) you probably want Process if you just need a child process to see the variable

如果您只需要子进程来查看变量,那么您可能需要4个“集合”(系统,用户,易失性和进程)进程

#3


You are getting the system environment variables. I suspect you simply don't have permission to modify them; you could try changing this to the user environment variables.

您正在获取系统环境变量。我怀疑你根本没有权限修改它们;您可以尝试将其更改为用户环境变量。

Also I don't know whether the argument to Environment() is case-sensitive or not. MS's documentation uses "System" instead of "SYSTEM". Might make a difference but I don't know for sure.

另外我不知道Environment()的参数是否区分大小写。 MS的文档使用“系统”而不是“系统”。可能会有所作为,但我不确定。