我可以在 vbscript WSH 脚本中获取环境变量吗?

发布于 2024-07-22 00:49:32 字数 136 浏览 10 评论 0 原文

是否可以读取 Windows 脚本主机 (WSH) VBS 脚本中的系统环境变量?

(我正在使用 Windows Scripting Host 为 Cruise Control 的任务编写 VBScript,并希望获取项目构建 URL。)

Is is possible to read system environment variables in a Windows Scripting Host (WSH) VBS script?

(I am writing a VBScript using Windows Scripting Host for task for a Cruise Control and want to pick up the project build URL.)

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

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

发布评论

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

评论(5

音盲 2024-07-29 00:49:32
Set WshShell = CreateObject("WScript.Shell")    
Set WshEnv = WshShell.Environment
WScript.Echo "WINDIR=" & WshEnv.Item("WINDIR")  & vbCrLf & vbCrLf   
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo "Environment System:"              & vbCrLf & _ 
         "..............................................."

For Each IEnv In WshShell.Environment("System")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment User:"       & vbCrLf & _   
        "..............................................."

For Each IEnv In WshShell.Environment("User") 
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Volatile:"   & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Volatile")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Process:"    & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Process")
    WScript.Echo IEnv
Next
Set WshShell = CreateObject("WScript.Shell")    
Set WshEnv = WshShell.Environment
WScript.Echo "WINDIR=" & WshEnv.Item("WINDIR")  & vbCrLf & vbCrLf   
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo "Environment System:"              & vbCrLf & _ 
         "..............................................."

For Each IEnv In WshShell.Environment("System")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment User:"       & vbCrLf & _   
        "..............................................."

For Each IEnv In WshShell.Environment("User") 
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Volatile:"   & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Volatile")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Process:"    & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Process")
    WScript.Echo IEnv
Next
聚集的泪 2024-07-29 00:49:32

现有的答案都很有帮助,但让我尝试一个务实的总结

通常,您需要当前进程的环境变量定义

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%")

这相当于(请注意变量名称周围缺少 %):

CreateObject("WScript.Shell").Environment("Process").Item("TEMP")

警告:不要省略 ("Process)部分:如果这样做,您将获得变量的系统范围的定义;见下文。

.ExpandEnvironmentStrings在概念上更简单、更灵活:它可以使用嵌入%-封闭)环境变量引用来扩展任意字符串,例如:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("My name is %USERNAME%")

在极少数情况下,您可以必须特定范围访问环境变量定义(当前进程除外)

  sScope = "System" ' May be: "Process", "User", "Volatile", "System"
  CreateObject("WScript.Shell").Environment(sScope).Item("TEMP")

注意:如上所述,省略范围参数默认为系统 。 范围

警告:以这种方式访问​​值不会扩展它:环境变量值可以嵌套:它们可以引用到其他环境变量。
在上面的示例中,返回值为 %SystemRoot%\TEMP,其中包含对 %SystemRoot%未扩展引用。
要扩展结果,请将其传递给 .ExpandEnvironmentStrings(),如上所示。

The existing answers are all helpful, but let me attempt a pragmatic summary:

Typically, you want the current process's definition of an environment variable:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%")

This is the equivalent of (note the absence of % around the variable name):

CreateObject("WScript.Shell").Environment("Process").Item("TEMP")

Caveat: Do not omit the ("Process) part: if you do, you'll get the system scope's definition of the variable; see below.

.ExpandEnvironmentStrings is conceptually simpler and more flexible: It can expand arbitrary strings with embedded (%-enclosed) environment-variable references; e.g.:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("My name is %USERNAME%")

On rare occasions you may have to access environment-variable definitions from a specific scope (other than the current process's).

  sScope = "System" ' May be: "Process", "User", "Volatile", "System"
  CreateObject("WScript.Shell").Environment(sScope).Item("TEMP")

Note: As stated above, omitting the scope argument defaults to the System scope.

Caveat: Accessing a value this way does not expand it: Environment-variable values can be nested: they can refer to other environment variables.
In the example above, the return value is %SystemRoot%\TEMP, which contains the unexpanded reference to %SystemRoot%.
To expand the result, pass it to .ExpandEnvironmentStrings(), as demonstrated above.

粉红×色少女 2024-07-29 00:49:32

这是一个示例(取自 此处):

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp

Here's an example (taken from here):

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp
虐人心 2024-07-29 00:49:32

来自此处。 ..

Set WshShell = WScript.CreateObject("WScript.Shell")

Set WshProccessEnv = WshShell.Environment("Process")
Set WshSysEnv = WshShell.Environment("System")

Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
Wscript.Echo WshProccessEnv("Path")

此外,TechNet 上有更多详细信息

From here ...

Set WshShell = WScript.CreateObject("WScript.Shell")

Set WshProccessEnv = WshShell.Environment("Process")
Set WshSysEnv = WshShell.Environment("System")

Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
Wscript.Echo WshProccessEnv("Path")

Also, much more detail on TechNet.

手长情犹 2024-07-29 00:49:32

这对我有用:

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName

或者从 shell:

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

或者从环境变量(它应该可以工作,但是当我测试时它是错误的!):

Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
MsgBox "USERNAME=" & WshEnv.Item("USERNAME")

This works for me:

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName

or from the shell:

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

or from environment variable (it should work, but when i tested it was wrong!):

Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
MsgBox "USERNAME=" & WshEnv.Item("USERNAME")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文