InterLinked
InterLinked

Reputation: 1403

How can I use the Computer Name Environment Variable in VBS with MsgBox?

I am trying to create my own custom dialogs for Windows, making them from scratch using shortcuts/wscript/and VBS to make them as realistic as possible. For some of my errors/dialogs, I want to use the %computername% variable in a dialog to make it seem even more realistic - like "Drive C: has been formatted on %computername% I've used such variables with batch files before, and it's always worked. I realize that VBS is a different language with different syntax, but none of the examples that I have tried have worked.

This is what I originally tried doing:

Set WshShell = WScript.CreateObject( "WScript.Shell" )
x=msgbox("Windows has reformatted Drive C: on %computername% successfully.", 0+64, "Hard Disk Reformat Successful")

Obviously that didn't work, and I get the following: Using %computername% as variable

I read up on using VBS enviornment variables a little. But none of the solutions I found worked.

Here are some of the resources I used that didn't work - I have listed the resource I used, the code I used, and a picture of the result:

1. Stack Overflow

     Set WshShell = WScript.CreateObject( "WScript.Shell" )
     dim oFso, oShell, oShellEnv, computerName, target, source
     const overwrite = true
     set oFso      = CreateObject("Scripting.FileSystemObject")
     set oShell    = WScript.CreateObject("WScript.Shell")
     set oShellEnv = oShell.Environment("Process")
     computerName  = oShellEnv("ComputerName")
     x=msgbox("Windows has reformatted Drive C: on computerName successfully.", 0+64, "Hard Disk Reformat Successful")

enter image description here

2. Stack Overflow (different page) and ss64

    dim strValue
    Set WshShell = WScript.CreateObject( "WScript.Shell" )
    Set objShell = CreateObject("WScript.Shell")
    objShell.Environment("computername") = "This is some data to share"
    strValue = objShell.Environment("VOLATILE")("MyVariable")
    x=msgbox("Windows has reformatted Drive C: on strValue successfully.", 0+64, "Hard Disk Reformat Successful")

enter image description here

3. Google Groups

    Set WshShell = WScript.CreateObject( "WScript.Shell" )
    Set wshShell = Wscript.CreateObject( "Wscript.Shell" )
    WScript.Echo "HOSTNAME: " & wshShell.ExpandEnvironmentStrings
    ( "%COMPUTERNAME%" )
    WScript.Echo "DOMAIN  : " & wshShell.ExpandEnvironmentStrings
    ( "%USERDOMAIN%" )
    WScript.Echo ""
    strServer=wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
    x=msgbox("Windows has reformatted Drive C: on strServer successfully.", 0+64, "Hard Disk Reformat Successful")

enter image description here

Am I doing something wrong here? Can anyone shed some light onto this? This seems like a fairly trivial issue and I feel that you should be able to integrate environment variables into a dialog box (I've seen it natively in Windows before.) I have been working with batch files for a long time but am relatively new to VBS scripting so I apologize if I made a really dumb mistake somewhere or something,

I am looking for any working solution. As long as I see %computername% where that is the actual local hostname, rather than computerName or the name of the variable, that is great.

Thanks for all of your help in advance!

EDIT: Response to @Lankymart

Using this code:

set WshShell = WScript.CreateObject( "WScript.Shell" )
Set wshShell = Wscript.CreateObject( "Wscript.Shell" )
WScript.Echo "HOSTNAME: " & wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
WScript.Echo "DOMAIN  : " & wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
WScript.Echo ""
strServer=wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
x=msgbox("Windows has reformatted Drive C: on strServer successfully.", 0+64, "Hard Disk Reformat Successful")

gives me this:

enter image description here

That is better but not quite what I wanted. If I click OK, then I recieve the domain, and then an empty box, and then my original message with strServer where computername should be

Upvotes: 1

Views: 3067

Answers (3)

rory.ap
rory.ap

Reputation: 35318

Your main issue looks to be with string concatenation. You cannot "inject" a variable's value into a string literal simply by using its name. Let's look at your second example:

 Set WshShell = WScript.CreateObject( "WScript.Shell" )
 dim oFso, oShell, oShellEnv, computerName, target, source
 const overwrite = true
 set oFso      = CreateObject("Scripting.FileSystemObject")
 set oShell    = WScript.CreateObject("WScript.Shell")
 set oShellEnv = oShell.Environment("Process")
 computerName  = oShellEnv("ComputerName")
 x=msgbox("Windows has reformatted Drive C: on computerName successfully.", 0+64, "Hard Disk Reformat Successful")

On the last line, you're attempting to include the value of the computerName variable in the string literal, but VBScript doesn't work that way. What you need to do is break the literal string up and concatenate a couple strings together along with the variable in the middle, like this:

x=msgbox("Windows has reformatted Drive C: on " & computerName & " successfully.", 0+64, "Hard Disk Reformat Successful")

Upvotes: 2

user692942
user692942

Reputation: 16682

Again you are not following advice you have already been given, as it stands that example will fail because the one statement spans multiple lines causing the

Microsoft VBScript compilation error: Expected statement

error.

Just place the statements on one line per statement to fix this

Set wshShell = Wscript.CreateObject( "Wscript.Shell" )
WScript.Echo "HOSTNAME: " & wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
WScript.Echo "DOMAIN  : " & wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
WScript.Echo ""
strServer=wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
x=msgbox("Windows has reformatted Drive C: on strServer successfully.", 0+64, "Hard Disk Reformat Successful")

or you need to use a Line Continuation Character (_) to allow the statement to span multiple lines.

You also are still using numeric values instead of named constants which has already been pointed out in the other question.


Without us having to second guess you obviously want to output strServer variable in the last statement which you can do using String Concatenation

strServer=wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
x=msgbox("Windows has reformatted Drive C: on " & strServer & "successfully.", 0+64, "Hard Disk Reformat Successful")

Upvotes: 1

user6017774
user6017774

Reputation:

You are trying to mimic a batch file. VBS has it's own ways. VBS ways are more reliable.

From Help at https://www.microsoft.com/en-au/download/details.aspx?id=2764

     Set WshNetwork = WScript.CreateObject("WScript.Network")
     WScript.Echo "Domain = " & WshNetwork.UserDomain
     WScript.Echo "Computer Name = " & WshNetwork.ComputerName
     WScript.Echo "User Name = " & WshNetwork.UserName

The problem you are having is strings and variables need to be joined using &. The only special meaning within a VB/VBS string is two quotes ("") for a single one. Everything else is literal, unlike C based languages (and in a different way batchfile).

msgbox "Windows has reformatted Drive C: on " & strServer & " Successfully."

Upvotes: 3

Related Questions