Search code examples
xmlvbscriptenvironment-variables

Trouble using an environment variable's value in VBS


I'm new to VBScript and wrote a little script that can modify an XML file; but I am having trouble putting the computer name in the XML.

I got the computer name from the HOST environment variable using these lines:

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

However, now I am unclear how to put it in a few paths inside the XML – meaning I want the computer name to be everywhere you see %HOST% below:

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

%HOST% in the above strings did not expand into the HOST environment variable's value as I hoped. What am I missing?

This is my full script:

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")

Solution

  • You just need to assign the expanded HOST environment variable to a local variable and prepend it to the strings in which you want to use it:

    Set wshShell = CreateObject( "WScript.Shell" )
    host = wshShell.ExpandEnvironmentStrings( "%HOST%" )
    wshShell = Nothing
    
    
    sWebcastFlash = host & ":port/webcast/"
    sWebcastInfra = host & "/webcast/"
    sWebcastTelephone = host & ":port/telephone/"
    

    Edit:

    @Tomalak raised a good point in his comment. For the sake of completeness, you could also accomplish this with a call to ExpandEnvironmentStrings for each string in which you want to use the HOST environment variable's value:

    Set wshShell = CreateObject( "WScript.Shell" )
    
    sWebcastFlash = wshShell.ExpandEnvironmentStrings( "%HOST%:port/webcast/" )
    sWebcastInfra = wshShell.ExpandEnvironmentStrings( "%HOST%/webcast/" )
    sWebcastTelephone = wshShell.ExpandEnvironmentStrings( "%HOST%:port/telephone/" )
    
    wshShell = Nothing
    

    As @Tomalak further pointed out, this involves a bit more code: it is needlessly repetitive in my view, but TMTOWTDI.