9
votes

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.

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.

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.

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.

1
I would strongly recommend switching to PowerShell instead of VBScript.Ansgar Wiechers

1 Answers

11
votes

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
    
  • changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).

  • change the engine via the script host option (i.e. as per http://support.microsoft.com/kb/245254)

    cscript //h:cscript //s
    
  • Or you can add a few lines to the start of your script to force it to switch "engine" from wscript to cscript - see http://www.robvanderwoude.com/vbstech_engine_force.php (copied below):

    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