在 VBS 中使用环境变量值时出现问题

问题描述 投票:0回答:2

我是 VBScript 新手,编写了一个可以修改 XML 文件的小脚本;但我在将计算机名称放入 XML 时遇到问题。

我使用以下几行从

HOST
环境变量中获取了计算机名称:

 Set wshShell = CreateObject( "WScript.Shell" )
 WScript.Echo wshShell.ExpandEnvironmentStrings( "HOST=%HOST%" )

但是,现在我不清楚如何将其放入 XML 内的几个路径中 - 这意味着我希望计算机名称出现在您在下面看到的

%HOST%
处:

sWebcastFlash = "%HOST%:port/webcast/"
sWebcastInfra = "%HOST%/webcast/"
sWebcastTelephone = "%HOST%:port/telephone/"
上面字符串中的

%HOST%
并没有像我希望的那样扩展到
HOST
环境变量的值。我错过了什么?

这是我的完整脚本:

Set wshShell = CreateObject( "WScript.Shell" )
WScript.Echo wshShell.ExpandEnvironmentStrings( "HOST=%HOST%" )
'wshShell = Nothing


sWebcastFlash = "%HOST%:port/webcast/"
sWebcastInfra = "%HOST%/webcast/"
sWebcastTelephone = "%HOST%:port/telephone/"

'Create XMLDoc object
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "D:\Configuration\developer\developer-definitions.xml"

Set nWebcastFlash = xmlDoc.selectSingleNode("//clusters/cluster/servers/server/webcast-fms-url-for-flash")
Set nWebcastInfra = xmlDoc.selectSingleNode("//clusters/cluster/servers/server/webcast-fms-url-for-infra")
Set nWebcastTelephone = xmlDoc.selectSingleNode("//clusters/cluster/servers/server/webcast-fms-telephone-preview-url")
Set nWebcamUrl = xmlDoc.selectsinglenode ("//clusters/cluster/servers/server/webcam-self-test-url")
Set nHostName = xmlDoc.selectsinglenode ("//clusters/cluster/servers/server/host-name")
Set nHostAddress = xmlDoc.selectsinglenode ("//clusters/cluster/servers/server/host-address")
Set nAudUrl = xmlDoc.selectsinglenode ("//media-definitions/media-servers/on-demand-media-url/url-list/url/aud-url")
Set nFlashUrl = xmlDoc.selectsinglenode ("//flash-server-chat-list-url/flash-server-url")
Set nWebcamTest = xmlDoc.selectsinglenode ("//flash-server-webcam-self-url/webcam-self-test")

'Set the text node with the new value
nWebcastFlash.text = sWebcastFlash
nWebcastInfra.text = sWebcastInfra
nWebcastTelephone.text = sWebcastTelephone
'Save the xml document with the new settings.
strResult = xmldoc.save("D:\out.xml")
xml vbscript environment-variables
2个回答
3
投票

您只需将扩展的

HOST
环境变量分配给局部变量并将其添加到您要使用它的字符串之前:

Set wshShell = CreateObject( "WScript.Shell" )
host = wshShell.ExpandEnvironmentStrings( "%HOST%" )
wshShell = Nothing


sWebcastFlash = host & ":port/webcast/"
sWebcastInfra = host & "/webcast/"
sWebcastTelephone = host & ":port/telephone/"

编辑:

@Tomalak 在他的评论中提出了一个很好的观点。为了完整起见,您还可以通过对要在其中使用

ExpandEnvironmentStrings
环境变量值的每个字符串调用
HOST
来完成此操作:

Set wshShell = CreateObject( "WScript.Shell" )

sWebcastFlash = wshShell.ExpandEnvironmentStrings( "%HOST%:port/webcast/" )
sWebcastInfra = wshShell.ExpandEnvironmentStrings( "%HOST%/webcast/" )
sWebcastTelephone = wshShell.ExpandEnvironmentStrings( "%HOST%:port/telephone/" )

wshShell = Nothing

正如 @Tomalak 进一步指出的那样,这涉及更多的代码:在我看来,这是不必要的重复,但是 TMTOWTDI。


0
投票

这是@J0e3gan答案的评论,但我无法添加评论...

我在带有 ExpandEnvironmentStrings 的 Wine 下的 CScript 中遇到了一个绊脚石:它给出了一个以 null 结尾的字符串,然后该字符串无法与左侧的其他字符串正常连接,因为它的右端有终止符零字符。如果您在连接从 ExpandEnvironmentStrings 获取的字符串时遇到问题,请尝试在获取环境变量后删除空终止符,如下所示:

' Get environment variable value:
Set wshShell = CreateObject( "WScript.Shell" )
host = wshShell.ExpandEnvironmentStrings( "%HOST%" )
wshShell = Nothing

' Removing the null terminator character from right end:
cTestChar=Asc(Right(host,1))
Do Until ((Len(host)=0) OR (cTestChar<>0))
  host=Left(host,Len(host)-1)
  cTestChar=Asc(Right(host,1))
Loop    

' Using the value to build other paths:    
sWebcastFlash = host & ":port/webcast/"
sWebcastInfra = host & "/webcast/"
sWebcastTelephone = host & ":port/telephone/"

希望它能为某人节省一些时间......

© www.soinside.com 2019 - 2024. All rights reserved.