Writing proxy launcher in VBScript/JScript for console application

105 views Asked by At

I want to alter some arguments passed to application by wrapping it into inspectable script.

CMD.exe is out of list because it damages original arguments list (=, , & ; are treated as command separator, rendering --opt=val into --opt + val).

I though about JScript but was frustrated by the fact that both Wscript.Shell with Run + Exec & Shell.Application with ShellExecute create new window instead of attaching to existing console.

It is vital for wrapper to pass STDIO control to launched app. Regular Batch files follow such semantic, CLI pipe to the app continue to work even if the app is called through .cmd wrapper.

1

There are 1 answers

0
leeharvey1 On

Are you looking for something like this?

launchApplication.vbs

Dim args : args = ""
For Each arg In WScript.Arguments
    If ("" = args) Then
        args = arg
    Else
        args = args & " " & arg
    End If
Next

WScript.StdOut.WriteLine "You are using these args: " & args

Dim o, e
With WScript.CreateObject("WScript.Shell")
    With .Exec(.ExpandEnvironmentStrings("%COMSPEC% /c application.exe " & args))
        o = .StdOut.ReadAll()
        e = .StdErr.ReadAll()
    End With
End With

WScript.StdOut.WriteLine "StdOut: " & o
WScript.StdOut.WriteLine "StdErr: " & e

Then invoke it using cscript.exe. For example:

cscript.exe launchApplication.vbs --opt=val

Assuming your console application.exe actually accepts --opt=val, then the above script should be capable of capturing the command-line args used to call application.exe, as well as the stdout and stderr from application.exe.

If needed, it can also be modified to alter the command-line arguments, prior to executing application.exe.