可以通过 Windows 登录脚本设置系统环境变量吗?

发布于 2024-07-13 19:05:56 字数 194 浏览 3 评论 0原文

我有一个 MSI 打包的应用程序,该应用程序通过组策略对象 (GPO) 从 Windows 2003 域服务器部署到网络中的所有 XP 客户端计算机。

该应用程序读取其配置的两个环境变量(要与之通信的服务器 IP),并且似乎我们还希望通过 GPO 样式设置或登录脚本将此配置推送到所有桌面。

在桌面网络中设置环境变量的最佳方法是什么?

I have an MSI-packaged application that is being deployed via Group Policy Objects (GPO) from a Windows 2003 Domain Server to all the XP client machines in the network.

This application reads two environment variables for its configuration (which server IPs to talk to) and it seems like we'd also want to push this configuration via a GPO style setting or Login script to all the desktops.

What is the best approach for setting environment variables across a network of desktops?

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

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

发布评论

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

评论(3

初与友歌 2024-07-20 19:05:56

我的研究表明有四种方法可以做到这一点。 我从 Microsoft 登录脚本文档页面开始,然后从那里展开

登录脚本批处理文件

Windows Server 2000、2003、2008

登录批处理文件 (.BAT) 脚本只是 CMD 窗口的临时实例,一旦登录窗口关闭,其中设置的环境变量就会消失。

set MYVAR=MyValue

由于上述原因将不起作用。

因此,或者,我可以尝试通过直接写入注册表来设置变量,就像系统环境变量一样:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MYVAR /t REG_EXPAND_SZ /d MyValue

或者像这样写入用户环境变量:

reg add HKCU\Environment /v MYVAR /t REG_EXPAND_SZ /d MyValue 

这里的缺点是变量虽然写入注册表,但不会被读取直到下次登录时我才能看到所有内容。 在用户重新登录之前,新的 CMD 窗口不会显示它们的踪迹。

登录脚本 WSH VBS 文件

Windows Server 2000、2003、2008

通过 Visual Basic 脚本 (VBS) 登录脚本,您可以使用更具编程性的方法来访问环境变量。 这看起来是我最可行的方法。 此示例将附加到 PATH 的末尾

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
WshEnv("Path") = WshEnv("Path") & ";M:\DB\whatever\"

这个例子只是设置变量。

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
WshEnv("MYVAR") = "MyNewValue"

这种方法产生的变量可通过 CMD 窗口立即使用。 不需要像批处理文件注册表写入那样重新启动。

ADM 文件

Windows Server 2000、2003、2008

ADM 文件是向组策略编辑器公开设置的自定义功能的一种方法。 让它们在域控制器上安装并可见似乎很棘手,所以我跳过这个选项。

有关 ADM 文件位置的 Microsoft 支持 TechNet 参考。
另一个有关 ADM 文件以及使用它们来设置注册表设置的文章。
ADM 文件上的 Tom 硬件。

---- set.adm ---- 
CLASS MACHINE 
CATEGORY "Environment" 
POLICY "Self dfined variables" 
KEYNAME "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" 
PART "Set MyVar1 =" EDITTEXT 
DEFAULT "MyValue1" 
VALUENAME MyVar1 ; EXPANDABLETEXT 
; add expandabletext if it can contain Variables itself 
END PART 
END POLICY 
END CATEGORY 
---- set.adm ----

组策略首选项 ( GPP)

Windows Server 2008

Windows Server 2008 h作为一项新功能,称为环境组策略首选项的扩展。 它允许您方便地设置原本需要复杂批处理脚本的内容。 公开的新项目包括注册表值、环境变量等。 此处提供了快速操作指南

我无法使用此选项,因为我的客户没有 Windows Server 2008。

摘要

请根据您作为 Windows 管理员的经验告诉我,其中哪一个最有效以及原因。 我只是一名桌面开发人员,需要管理员的洞察力。

My research says there are four ways to do this. I started at the Microsoft Logon Script documentation pages and fanned out from there.

Login Script Batch File

Windows Server 2000, 2003, 2008

Login batch file (.BAT) scripts are just a temporary instance of a CMD window, and the environment variables set in there go away as soon as the login window closes.

set MYVAR=MyValue

Won't work for the aforementioned reason.

So, alternatively, I can try to set the variable via directly writing to the registry like so for a System Environment Variable:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MYVAR /t REG_EXPAND_SZ /d MyValue

or to the User Environment Variables like so:

reg add HKCU\Environment /v MYVAR /t REG_EXPAND_SZ /d MyValue 

The drawback here is that the variables, though written to registry, are not read until the next login for all I can see. A new CMD window shows no trace of them until the user re-logs-in.

Login Script WSH VBS File

Windows Server 2000, 2003, 2008

With a Visual Basic Script (VBS) login script, you can use a more programmatic method to access the environment variables. This is looking like my most viable approach. This example would append to the end of PATH.

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
WshEnv("Path") = WshEnv("Path") & ";M:\DB\whatever\"

This example would just set the variable.

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("SYSTEM")
WshEnv("MYVAR") = "MyNewValue"

This approach yields variables that are immediately available via a CMD window. No reboot is required like the batch file registry writes.

ADM File

Windows Server 2000, 2003, 2008

ADM files are a way to expose custom functionality of settings to the Group Policy Editor. It seems tricky to get them installed and visible on the domain controller so I'm jumping over this option.

Microsoft Support TechNet Reference on ADM File Locations.
Another article about ADM files and using them to set Registry settings.
Tom's Hardware on ADM Files.

---- set.adm ---- 
CLASS MACHINE 
CATEGORY "Environment" 
POLICY "Self dfined variables" 
KEYNAME "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" 
PART "Set MyVar1 =" EDITTEXT 
DEFAULT "MyValue1" 
VALUENAME MyVar1 ; EXPANDABLETEXT 
; add expandabletext if it can contain Variables itself 
END PART 
END POLICY 
END CATEGORY 
---- set.adm ----

Group Policy Preferences (GPP)

Windows Server 2008

Windows Server 2008 has a new feature called the Environment Extensions for the Group Policy Preferences. It allows you to conveniently set what otherwise required complex batch scripts. The new items exposed include registry values, environment variables, and more. A quick how-to guide is available here.

I can't use this option because my clients don't have Windows Server 2008.

Summary

Please tell me based on your experiences as Windows Administrators which of these works best and why. I'm just a desktop developer, and need an admin's insight.

泪痕残 2024-07-20 19:05:56

为什么不能将此配置嵌入到 MSI 的属性表中(构建后,使用转换),然后从那里读取? 这将更有意义......启动 Orca,添加几个属性,保存转换并通过应用转换的 GPO 进行部署。

编辑:只需重新阅读这个问题...然后将设置部署到注册表并让应用程序从那里读取,而不是设置环境变量。 从管理员的角度来看,为一个应用程序设置全局环境变量没有意义。

Why can't you embed this configuration into the Property table of the MSI (post-build, using a transform) and then read from there? This would make much more sense... fire up Orca, add a couple of properties, save a transform and deploy via GPO with transform applied.

Edit: Just re-read this question... then deploy settings to the registry and have the application read from there, rather than setting environment variables. Setting global environment variables for one application doesn't make sense for an administrators point of view.

愁以何悠 2024-07-20 19:05:56

您始终可以通过登录脚本设置环境变量。 当然!

我们就是这样做的:

不要使用“set”,因为它不会被接管到用户环境中。 用“set”设置的变量仅在登录脚本运行期间有效。

使用“setx”。

因此,要在用户环境中设置变量

setx MYSPECIALVAR THIS_IS_THE_VALUE

setx Softwaresource  \\\this\is\the\value\of\the\variable

(注意:没有 = 符号,因为 set MYSPECIALVAR=THIS_IS_THE_VALUE 就是这种情况)

如果用户具有管理权限 -您还可以在全局系统环境中设置变量

setx MYSPECIALVAR THIS_IS_THE_VALUE /m

(这就是我们使用安装脚本和管理权限在整个 PC 上部署变量的方式)

You can always set environment variable through your login script. Of course!

That's how we do it:

Don't use "set" as it is not taken over into the user-environment. Variables set with "set" are just valid during the login-script-running-time.

Use "setx".

So, to set a variable within the users envrionment

setx MYSPECIALVAR THIS_IS_THE_VALUE

setx Softwaresource  \\\this\is\the\value\of\the\variable

(Note: There is no = sign as it would be the case with set MYSPECIALVAR=THIS_IS_THE_VALUE)

If the user has administration-rights on the PC you could also set the variables within the global-system-envrionment with

setx MYSPECIALVAR THIS_IS_THE_VALUE /m

(Thats the way we deploy variables throughout the PCs with installationscripts and administrative-rights)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文