【发布时间】:2016-04-12 12:51:51
【问题描述】:
我正在尝试为 Windows 创建自己的自定义对话框,使用快捷方式/wscript/和 VBS 从头开始制作它们,以使它们尽可能逼真。对于我的一些错误/对话框,我想在对话框中使用 %computername% 变量以使其看起来更加真实 - 例如“驱动器 C:已在 %computername% 上格式化,我之前已将此类变量与批处理文件一起使用,并且一直有效。我意识到 VBS 是一种具有不同语法的不同语言,但我尝试过的示例都没有奏效。
这是我最初尝试做的:
Set WshShell = WScript.CreateObject( "WScript.Shell" )
x=msgbox("Windows has reformatted Drive C: on %computername% successfully.", 0+64, "Hard Disk Reformat Successful")
我阅读了一些关于使用 VBS 环境变量的信息。但我找到的解决方案都没有奏效。
以下是我使用的一些无效资源——我列出了我使用的资源、我使用的代码以及结果图片:
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) 和 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")
我在这里做错了吗?任何人都可以对此有所了解吗?这似乎是一个相当微不足道的问题,我觉得您应该能够将环境变量集成到对话框中(我以前在 Windows 中原生地看到过。)我一直在处理批处理文件,但相对较新对于 VBS 脚本,如果我在某处或某处犯了一个非常愚蠢的错误,我深表歉意,
我正在寻找任何可行的解决方案。只要我看到 %computername% 是实际的本地主机名,而不是 computerName 或变量的名称,那就太好了。
提前感谢您的所有帮助!
编辑: 回复@Lankymart
使用此代码:
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")
给我这个:
这更好,但不是我想要的。如果我单击“确定”,那么我会收到域,然后是一个空框,然后是我的原始消息,其中包含 strServer,其中计算机名应该是
【问题讨论】:
-
这是问题的延续,而不是您最初提出的问题。如果你想在字符串中输出变量,你可以使用String Concatenation,Stack Overflow 上有很多例子。
-
以后请阅读How to create a Minimal, Complete, and Verifiable example,您会为自己和我们节省很多浪费的时间。
-
您发现的所有示例都有效,只是您修改了每个示例,这意味着它们不起作用,
Example 2.您无法为系统环境变量赋值,Example 3.您一个语句可以跨越多行,All Examples.你不能将变量注入到必须连接的字符串中。
标签: windows vbscript runtime-error environment-variables msgbox