【问题标题】:How can I use the Computer Name Environment Variable in VBS with MsgBox?如何将 VBS 中的计算机名称环境变量与 MsgBox 一起使用?
【发布时间】: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 环境变量的信息。但我找到的解决方案都没有奏效。

以下是我使用的一些无效资源——我列出了我使用的资源、我使用的代码以及结果图片:

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

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

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

我在这里做错了吗?任何人都可以对此有所了解吗?这似乎是一个相当微不足道的问题,我觉得您应该能够将环境变量集成到对话框中(我以前在 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 ConcatenationStack Overflow 上有很多例子。
  • 以后请阅读How to create a Minimal, Complete, and Verifiable example,您会为自己和我们节省很多浪费的时间。
  • 您发现的所有示例都有效,只是您修改了每个示例,这意味着它们不起作用,Example 2.您无法为系统环境变量赋值,Example 3.您一个语句可以跨越多行,All Examples. 你不能将变量注入到必须连接的字符串中。

标签: windows vbscript runtime-error environment-variables msgbox


【解决方案1】:

您正在尝试模仿批处理文件。 VBS有它自己的方式。 VBS方式更可靠。

来自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

您遇到的问题是字符串和变量需要使用& 连接。 VB/VBS 字符串中唯一的特殊含义是两个引号 ("") 用于单个引号。其他一切都是文字,不像基于 C 的语言(并且以不同的方式批处理文件)。

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

【讨论】:

    【解决方案2】:

    您的主要问题似乎与字符串连接有关。您不能仅通过使用变量的名称将变量的值“注入”到字符串文字中。让我们看看你的第二个例子:

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

    在最后一行,您尝试将computerName 变量的值包含在字符串文字中,但VBScript 不能这样工作。您需要做的是分解文字字符串并将几个字符串与中间的变量连接在一起,如下所示:

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

    【讨论】:

    • 这是其中一个示例的一个问题。根本问题是他们需要更好地理解 VBScript 语法。
    • @Lankymart -- 不,这是所有示例的主要问题,OP 问题的关键是“我如何显示计算机名称”
    • 所有示例都有不同的错误,无论 OP 尝试什么,它们失败的原因都完全不同。整个问题都跑题了。
    • @roryap 如果我按照您的指示进行操作,则会收到以下错误。我的对话框根本没有出现:行:2/字符:1/错误:类型不匹配:'oshellEnv',代码为 800A000D。如果我为 computerName 添加一个 dim 语句并将 Option Explicit 添加到顶部,我会收到相同的内容,除了第 3 行出现错误,未定义变量为 Wshshell,错误代码为 800A01F4。
    • @InterLinked -- 抱歉,为简洁起见,我从您的代码示例中省略了一些行,我认为您会意识到这一点。我已经更新了我的答案。它包含您的第二个代码示例,即您链接到第一个堆栈溢出帖子的代码示例。
    【解决方案3】:

    再说一遍,你不是following advice you have already been given,因为这个例子会失败,因为一个语句跨越多行导致

    Microsoft VBScript 编译错误:预期语句

    错误。

    只需将语句放在每个语句的一行即可解决此问题

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

    或者您需要使用行继续符(_) 以允许语句跨越多行。

    您还在使用数值而不是具有already been pointed out in the other question 的命名常量。


    不用我们再猜测,您显然想在最后一条语句中输出 strServer 变量,您可以使用 String Concatenation 来完成

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

    【讨论】:

    • 感谢提醒,我实现了这段代码,但是我的 msgbox 的其余部分不起作用。另外,您不能以任何一种方式使用仍然使用数值吗?重要还是有某种区别?我不知道为什么,但使用数字似乎更容易,除非有理由不这样做,否则我更愿意这样做
    • 您正在使用 WScript.Echo 输出每个语句,这将导致 (当使用 wscript.exe 而不是 cscript.exe 运行时) 在生成类似于 @ 的消息框中987654331@ 但选项较少。如果您只想显示一个 MsgBox 和所有文本,请先将字符串构建到一个变量中,然后在调用 MsgBox() 时使用该变量。
    • 我可以摆脱 Wscript.Echo 吗?
    • @InterLinked 有很多不同的方法可以解决这个问题,但我不会全部为你写。您给出的示例没有任何问题,除了语句跨越多行导致错误,如果您在cscript.exe 中运行上面的示例,您将看到我的意思是第一个语句将输出到控制台,最后一个语句将生成一个消息框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2017-08-11
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多