问题描述
我们目前使用 Windows 批处理 (DOS) 命令文件来控制我们的流程.要向控制台显示消息,我们将使用 ECHO 命令.这些消息将显示在我们的调度程序软件中,该软件以前是 Tivoli,现在是 CA WA Workstation\ESP.
We currently use Windows Batch (DOS) command files to control our process flow. To display messages to the Console, we would use the ECHO command. These messages would show up in our Scheduler software, which used to be Tivoli and now is CA WA Workstation\ ESP.
我想开始使用 VBS 文件而不是 CMD\BAT 文件,并试图弄清楚如何对控制台执行等效的 ECHO.
I would like to start using VBS files instead of CMD\BAT files and am trying to figure out how to do the equivalent of an ECHO to the console.
当我尝试使用 WScript.Echo 命令或写入标准输出时,消息会显示在这两者的对话框中,并且需要按下确定"按钮才能继续.毫不奇怪,当我通过调度程序在无人看管的情况下运行时,该作业会执行这些命令之一并挂起,因为没有人可以确定消息框.
When I try to use either the WScript.Echo command or write to Standard Out, the messages are displayed in dialog boxes for both and they require the OK button to be pushed to continue. Not surprisingly, when I run unattended though a scheduler, the job hits one of these commands and just hangs since there is no one to OK the messagebox.
SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")
WScript.echo("Test 2")
我意识到我可以使用脚本对象将消息写入日志文件,但如果提供的路径无效或由于权限不足,这可能会失败.此外,能够在调度程序中看到反馈写入非常方便.
I realize I could write the messages to a Log file using the Scripting object, but this could fail if an invalid path is provided or because of insufficient permissions. Besides, being able to see feedback write within the Scheduler is awfully convenient.
如何使用 VBScript 写入控制台?我在这里看到的其他帖子表明,由于上述原因而不起作用的上述方法是可行的方法.
How do I write to the Console using VBScript? I’ve seen other posts here that suggest that the above methods which didn't work for the reason describe above were the way to do it.
推荐答案
wscript.echo 是正确的命令 - 但是要输出到控制台而不是对话,您需要使用 cscript 而不是 wscript 运行脚本.
wscript.echo is the correct command - but to output to console rather than dialogue you need to run the script with cscript instead of wscript.
您可以通过以下方式解决此问题
You can resolve this by
像这样从命令行运行脚本:
running your script from command line like so:
cscript myscript.vbs
更改默认文件关联(或为要使用 cscript 运行的脚本创建新的文件扩展名和关联).
changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).
通过脚本主机选项更改引擎(即根据 http://support.microsoft.com/kb/245254)
change the engine via the script host option (i.e. as per 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 的 Batch ECHO 命令的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!