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