gpt4 book ai didi

vbscript - 使用 VBScript 的批量 ECHO 命令相当于什么?

转载 作者:行者123 更新时间:2023-12-02 21:28:03 24 4
gpt4 key购买 nike

我们目前使用 Windows Batch (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")

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

如何使用 VBScript 写入控制台?我在这里看到了其他帖子,这些帖子表明上述方法由于上述原因而不起作用,因此可以采取这种方法。

最佳答案

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

关于vbscript - 使用 VBScript 的批量 ECHO 命令相当于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13201405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com