Is is possible to read system environment variables in a Windows Scripting Host (WSH) VBS script?
是否可以在Windows Scripting Host(WSH)VBS脚本中读取系统环境变量?
(I am writing a VBScript using Windows Scripting Host for task for a Cruise Control and want to pick up the project build URL.)
(我正在使用Windows Scripting Host编写VBScript以获取Cruise Control的任务,并希望获取项目构建URL。)
5 个解决方案
#1
Here's an example (taken from here):
这是一个例子(取自这里):
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp
#2
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.
另外,有关TechNet的更多细节。
#3
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.:
.ExpandEnvironmentStrings在概念上更简单,更灵活:它可以使用嵌入的(%-enclosed)环境变量引用扩展任意字符串;例如。:
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.
注意:如上所述,省略scope参数默认为System范围。
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.
警告:以这种方式访问值不会扩展它:环境变量值可以嵌套:它们可以引用其他环境变量。在上面的示例中,返回值为%SystemRoot%\ TEMP,其中包含对%SystemRoot%的未展开引用。要展开结果,请将其传递给.ExpandEnvironmentStrings(),如上所示。
#4
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
#5
This works for me:
这对我有用:
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName
or from the shell:
或者来自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")
#1
Here's an example (taken from here):
这是一个例子(取自这里):
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp
#2
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.
另外,有关TechNet的更多细节。
#3
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.:
.ExpandEnvironmentStrings在概念上更简单,更灵活:它可以使用嵌入的(%-enclosed)环境变量引用扩展任意字符串;例如。:
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.
注意:如上所述,省略scope参数默认为System范围。
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.
警告:以这种方式访问值不会扩展它:环境变量值可以嵌套:它们可以引用其他环境变量。在上面的示例中,返回值为%SystemRoot%\ TEMP,其中包含对%SystemRoot%的未展开引用。要展开结果,请将其传递给.ExpandEnvironmentStrings(),如上所示。
#4
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
#5
This works for me:
这对我有用:
Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName
or from the shell:
或者来自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")