使用VBScript的Batch ECHO Command相当于什么?

Cha*_*adD 9 vbscript

我们目前使用Windows Batch(DOS)命令文件来控制我们的流程.要向控制台显示消息,我们将使用ECHO命令.这些消息将显示在我们的Scheduler软件中,该软件曾经是Tivoli,现在是CA WA Workstation\ESP.

我想开始使用VBS文件而不是CMD\BAT文件,并试图弄清楚如何对控制台执行相当于ECHO的操作.

当我尝试使用WScript.Echo命令或写入标准输出时,消息显示在两个对话框中,并且需要按下确定按钮才能继续.毫不奇怪,当我通过调度程序无人值守运行时,作业会命中其中一个命令而只是挂起,因为没有人可以确定消息框.

SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")

WScript.echo("Test 2")
Run Code Online (Sandbox Code Playgroud)

我意识到我可以使用Scripting对象将消息写入日志文件,但如果提供了无效路径或权限不足,则可能会失败.此外,能够在Scheduler中看到反馈写入非常方便.

如何使用VBScript写入控制台?我在这里看过其他帖子,表明上述方法因上述原因而无法正常工作.

Joh*_*van 11

wscript.echo是正确的命令 - 但要输出到控制台而不是对话框,您需要使用cscript而不是wscript运行脚本.

你可以解决这个问题

  • 从命令行运行脚本,如下所示:

    cscript myscript.vbs
    
    Run Code Online (Sandbox Code Playgroud)
  • 更改默认文件关联(或为要使用cscript运行的脚本创建新的文件扩展名和关联).

  • 通过脚本主机选项更改引擎(即根据http://support.microsoft.com/kb/245254)

    cscript //h:cscript //s
    
    Run Code Online (Sandbox Code Playgroud)
  • 或者您可以在脚本的开头添加几行以强制它将"引擎"从wscript切换到cscript - 请参阅http://www.robvanderwoude.com/vbstech_engine_force.php(复制如下):

    RunMeAsCScript
    
    'do whatever you want; anything after the above line you can gaurentee you'll be in cscript 
    
    Sub RunMeAsCScript()
            Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
            Set wshShell = CreateObject( "WScript.Shell" )
            strEngine = UCase( Right( WScript.FullName, 12 ) )
            If strEngine <> "\CSCRIPT.EXE" Then
                    ' Recreate the list of command line arguments
                    strArgs = ""
                    If WScript.Arguments.Count > 0 Then
                            For i = 0 To WScript.Arguments.Count
                                    strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i)) 
                            Next
                    End If
                    ' Create the complete command line to rerun this script in CSCRIPT
                    strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
                    ' Rerun the script in CSCRIPT
                    Set objDebug = wshShell.Exec( strCmd )
                    ' Wait until the script exits
                    Do While objDebug.Status = 0
                            WScript.Sleep 100
                    Loop
                    ' Exit with CSCRIPT's return code
                    WScript.Quit objDebug.ExitCode
            End If
    End Sub
    
    'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
    function QuoteIt(strTemp) 
            if instr(strTemp," ") then
                    strTemp = """" & replace(strTemp,"""","""""") & """"
            end if
            QuoteIt = strTemp
    end function
    
    Run Code Online (Sandbox Code Playgroud)

  • 我使用JS在同样的问题上偶然发现了.检查wscript/cscript在那里更加优雅:`if(!WScript.FullName.match("cscript.exe")){...}`.(或者可能在正则表达式中添加`$`,以防万一).我没有重新运行脚本 - 只是根据使用的引擎改变详细程度.顺便说一句,在上面的例子中:参数转义/引用怎么样?它是否适用于包含空格的参数?啊哈! (2认同)