1

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

Community
  • 1
  • 1
InterLinked
  • 1,247
  • 2
  • 18
  • 50
  • 1
    That is a continuation of the question, not what you initially asked. If you want to output variables inside a string you use [String Concatenation](https://msdn.microsoft.com/en-us/library/sx97884w(v=vs.84).aspx) of which there are plenty examples on [so]. – user692942 Apr 12 '16 at 13:14
  • In future please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), you would save yourself and us a lot of wasted time. – user692942 Apr 12 '16 at 13:26
  • All the examples you found worked, it's just you modified each of them in ways that meant they didn't work, `Example 2.` you can't assigned values to system environment variables, `Example 3.` you can have a statement span multiple lines and `All Examples.` you can't inject variables into strings they have to be concatenated. – user692942 Apr 12 '16 at 13:36

3 Answers3

3

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."
2

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")
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • That is one issue with one of the examples. Fundamental issue is they need to understand VBScript syntax better. – user692942 Apr 12 '16 at 13:12
  • @Lankymart -- No, it's the main issue with all of the examples, and the crux of the OP's question is "how do I display the computer name" – rory.ap Apr 12 '16 at 13:29
  • All the examples have different errors the reason they failed are completely different regardless of what the OP was attempting. The whole question is off-topic. – user692942 Apr 12 '16 at 13:31
  • @roryap If I follow your instructions, I receive the following error. My dialog does not appear at all: Line: 2/Char: 1/Error: Type mismatch: 'oshellEnv' with Code 800A000D. if I add a dim statement for computerName and add Option Explicit to the top, I recieve the same thing except with the error being on Line 3, the undefined variable being Wshshell, and the error code being 800A01F4. – InterLinked Apr 12 '16 at 16:29
  • @InterLinked -- My apologies, I left out some lines from your code sample for brevity and I assumed you would realize that. I've updated my answer. It contains your second code sample, the one where you linked to the first stack overflow post. – rory.ap Apr 12 '16 at 16:32
  • Thank you so much! Worked like a charm! – InterLinked Apr 12 '16 at 16:38
1

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")
Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Thanks for the reminder, I implemented this code but then the rest of my msgbox doesn't work. Also, can't you use still use numeric values either way? Does it matter or is there some sort of difference? I don't know why, but it seems easier to use numbers and I would prefer to do so unless there is a reason not to – InterLinked Apr 12 '16 at 13:01
  • You are using `WScript.Echo` to output each statement which will result *(when run with `wscript.exe` instead of `cscript.exe`)* in a Message Box being generated similar to `MsgBox()` but with less options. If you want just one `MsgBox` displayed with all the text in build the string up first into a variable then use that variable when calling `MsgBox()`. – user692942 Apr 12 '16 at 13:04
  • May I just get rid of Wscript.Echo? – InterLinked Apr 12 '16 at 13:05
  • @InterLinked There are lots of different ways of approaching this, but I'm not going to write it all for you. There is nothing wrong with the example you gave except the statements spanned multiple lines causing an error, if you run the sample above in `cscript.exe` you will see what I mean the first statements will output to the console and the last one will generate a message box. – user692942 Apr 12 '16 at 13:08