rulururu

post VBS-Battle: command line for output

December 10th, 2008

Filed under: General Programming, Windows — Kai @ 2:04 pm

From time to time I have battles with Visual Basic Script, which I usually avoid using.

I wanted console output because the process I’m starting should be unattended, rather than clicking through a bunch of MsgBoxes.

Typically, one prints in VBscript using a Wscript.echo("Hello, world!") line or some variant thereof. If you do not invoke with a cscript hello-world.vbs, you get a GUI/pop-up MsgBox, which I wish to avoid. I just wanted something to go right to the command line for output, without putting cscript at the beginning.
Wscript.StdOut.WriteLine dies, of course, if not also invoked with a cscript.

For different reasons I didn’t want to permanently set my default scripting host to cscript, either.

Probably the best solution to solve that problem is a simple stub in the script that will be called when it starts, which detects how the script was started, and re-starts it explicitly using cscript.exe if needed. It makes use of the wscript.fullname property (which is the path to the running host executable, either c:\windows\system32\cscript.exe or wscript.exe). If the script is running as wscript.exe, it will simply re-launch the script using cscript.exe and exit.
This way, if the local machine has wscript as the default host, it will immediately launch, detect that it was launched via wscript, and re-launch itself using wshell.run as a cscript. The local host doesn’t need to be reconfigured for this to work.

'this is at the start of your script
CheckStartMode
 
' This is somewhere else in your script
Sub CheckStartMode
     ' Returns the running executable as upper case from the last \ symbol
     strStartExe = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )
 
     If Not strStartExe = "CSCRIPT.EXE" Then
          ' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
          ' wscript.scriptfullname is the full path to the actual script
 
          set           oSh = CreateObject("wscript.shell")
          oSh.Run "cscript.exe """ & wscript.scriptfullname & """"
          wscript.quit
 
     End If
End Sub

The only disadvantage of that solution is that it closes the console window it opens as soon as the script is finished. I went through all of the options for intWindowStyle in the Run method of the WshShell object and none of them kept the spawned console open for more than a flash. Maybe there’s a hack for it, too.

1 Comment »

  1. Thanks for posting this. I needed this functionality for a logon script and explicitly do not want another command window popping up. Below is a modified version of your subroutine which ensures the CScript window stays hidden:

    ‘/////////////////////////////////////////////////////////////////////////////’
    Sub CheckStartMode
    ‘ Ensure script is launched in CScript.
    ‘/////////////////////////////////////////////////////////////////////////////’

    ‘ Returns the running executable as lowercase from the last \ symbol
    Dim strStartExe
    strStartExe = LCase(Mid(WScript.FullName, _
    InStrRev(WScript.FullName, “\”) + 1 ) )

    If Not strStartExe = LCase(”CScript.exe”) Then
    Const HIDDEN_WINDOW = 0

    Dim strComputer
    Dim intProcessID
    Dim objWMIService, objStartup, objConfig, objProcess

    strComputer = “.”

    Set objWMIService = GetObject(”winmgmts:\\” & strComputer & “\root\cimv2″)
    Set objStartup = objWMIService.Get(”Win32_ProcessStartup”)
    Set objConfig = objStartup.SpawnInstance_

    objConfig.ShowWindow = HIDDEN_WINDOW

    Set objProcess = GetObject(”winmgmts:\\” & strComputer & _
    “\root\cimv2:Win32_Process”)

    objProcess.Create “CScript.exe “”" & _
    WScript.ScriptFullName & “”"”, null, objConfig, intProcessID

    End If

    End Sub
    ‘/////////////////////////////////////////////////////////////////////////////’

    Comment by Ken Knicker — August 6, 2009 @ 12:21 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)