从VBScript重新读取环境变量

时间:2017-06-06 13:36:26

标签: vbscript scripting wix windows-installer environment-variables

我正在从MSI安装程序运行VBScript(使用WiX工具集创建)。此脚本使用PATH环境变量检查是否有其他软件 安装与否。安装程序还提供重试按钮,该按钮再次运行脚本。

现在的问题是:如果在我的安装程序运行时更改了PATH环境变量,我的脚本将不会检测到该更改。我的脚本仅在重新启动安装程序后使用PATH环境变量的新值。

所以问题是:如何强制安装程序进程使用VBScript更新其环境变量的副本?

修改

看起来this article解释了如何使用PowerScript解决问题。但我需要一个VBScript的解决方案。

2 个答案:

答案 0 :(得分:1)

With WScript.CreateObject("WScript.Shell")
    .Environment("PROCESS")("PATH") = .ExpandEnvironmentStrings(Replace( _ 
        .Environment("USER")("PATH") & ";" & .Environment("SYSTEM")("PATH"), ";;", ";" _ 
    ))
End With 

这将覆盖内存当前进程PATH环境变量,其中包含从usersystem环境变量的注册表中检索到的信息。

note :以前的代码只更新执行代码的[c|w]script进程的环境副本。它不会更新安装程序的环境副本(您无法更新其他进程环境)。

答案 1 :(得分:0)

或者,从jscript(仍然是cscript解释器,但是是jscript语言)

// Re-read PATH vars in case they've changed
var shell = new ActiveXObject("WScript.shell");
var newPath = (shell.Environment("USER")("PATH") + ";" + shell.Environment("SYSTEM")("PATH")).replace(/;;/g, ";");
// Expand any %STRINGS% inside path and set to the new running process
shell.Environment("PROCESS")("PATH") = shell.ExpandEnvironmentStrings(newPath);

shell.Exec('myprocess.exe'); // throws exception if not found