VBScript:Windows 系统环境变量,更改未反映在 CMD 提示符中

发布于 2024-11-05 03:33:29 字数 525 浏览 0 评论 0原文

我编写了一个 VB 脚本(.vbs)来添加 Windows 系统环境变量,如下所示,

set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\CATALINA_HOME", "C:\Tomcat5" , "REG_EXPAND_SZ"

当我在“系统属性 ->”中看到此环境变量时 ,高级->环境变量对话框,它显示了那里的环境变量。

但是,当我运行命令提示符并在那里键入“set”命令时,我在那里找不到该变量。 (我在执行 VBS 后启动了新的 CMD 提示符)

有些 CMD 提示符没有获取环境变量中的更改。

如果我重新启动计算机,那么我可以从 CMD 提示符访问环境变量。但是,我不希望用户在执行我的 vbs 和 cmd 提示符中的工作后重新启动系统。

有什么想法吗?

I have written a VB script (.vbs) to add a Windows System Environment variable, as shown below,

set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\CATALINA_HOME", "C:\Tomcat5" , "REG_EXPAND_SZ"

When I see this environment variable in System Properties -> Advanced -> Environment Variables dialog, It shows that environment variable there.

But, when I run the command prompt and type "set" command there, I don't find the variable there. (I started the new CMD prompt after execution of the VBS)

Some how CMD prompt is not getting the changes in environment variable.

If I restart the machine, then I can access the environment variable from CMD prompt. But, I don't want user to restart the system after executing my vbs and the work in cmd prompt.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-11-12 03:33:34

谢谢路易斯!

实际上,使用注册表添加/修改环境变量是一种有点原始的方式。这样做可能会造成一些不一致。

因此,理想的方法是使用专用于环境变量的集合 WshEnvironment。

现在,按照Luis的建议,我编写了以下脚本来添加系统环境变量,

Set wshshell = CreateObject("WScript.Shell")
Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv("1VINOD") = "1Vinod"
WshSysEnv("2VINOD") = "2Vinod"
Set WshSySEnv = Nothing

将此代码保存在vbs文件中,执行vbs文件。现在,您将在cmd提示符中获得2个环境变量,无需重新启动系统

用于删除变量的类似脚本,

Set wshshell = CreateObject("WScript.Shell")
Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv.Remove("1VINOD") 
WshSysEnv.Remove("2VINOD") 
Set WshSySEnv = Nothing

这也不需要任何重新启动/登录注销。

享受!

我已经在 XP 上测试过了,希望它也适用于 Windows 7。

Thanks Luis!

Actually, adding/modifying environment variable using registry is a bit raw way. We might be creating some inconsistencies by doing so.

So, the ideal way of doing is by using the collection which is dedicated to the environment variables, WshEnvironment.

Now, as suggested by Luis, I have written the following script to add system environment variable,

Set wshshell = CreateObject("WScript.Shell")
Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv("1VINOD") = "1Vinod"
WshSysEnv("2VINOD") = "2Vinod"
Set WshSySEnv = Nothing

Save this code in vbs file, execute the vbs file. Now, you will get the 2 environment variables in cmd prompt without restarting the system.

Similar script for removing the variables,

Set wshshell = CreateObject("WScript.Shell")
Dim WshSySEnv
Set WshSysEnv = wshshell.Environment("SYSTEM")
WshSysEnv.Remove("1VINOD") 
WshSysEnv.Remove("2VINOD") 
Set WshSySEnv = Nothing

This also does not require any restarts/logon-logoffs.

Enjoy!

I have tested it on XP, hope this works on Windows 7, too.

无人接听 2024-11-12 03:33:33

这是因为您要在注册表上“插入”一个永久值,要设置“实时”值,请使用流动代码,因为这两个建议都使用两个代码

  SET oShell = CREATEOBJECT("Wscript.Shell")
  SET wSystemEnv = oShell.Environment("SYSTEM")
  wSystemEnv("<Name>") = "<Value>"
  SET wSystemEnv = NOTHING
  SET oShell = NOTHING

that is because you are "inserting" a permanent value on the registry, to set a "live" value use the flowing code, for both prouposes use both codes

  SET oShell = CREATEOBJECT("Wscript.Shell")
  SET wSystemEnv = oShell.Environment("SYSTEM")
  wSystemEnv("<Name>") = "<Value>"
  SET wSystemEnv = NOTHING
  SET oShell = NOTHING
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文