9

我们目前使用 Windows 批处理 (DOS) 命令文件来控制我们的流程。要向控制台显示消息,我们将使用 ECHO 命令。这些消息将显示在我们的调度程序软件中,该软件以前是 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")

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

如何使用 VBScript 写入控制台?我在这里看到了其他帖子,这些帖子表明由于上述原因而不起作用的上述方法是这样做的方法。

4

1 回答 1

11

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

您可以通过以下方式解决此问题

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

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

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

    cscript //h:cscript //s
    
  • 或者,您可以在脚本的开头添加几行以强制将“引擎”从 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
    
于 2012-11-02T19:07:48.853 回答