Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why I cant get the output when running this Visual Basic Script as a Process
Process on the VB Script will validate a field, returns valid or not.

Script
VB
Dim Arg
Set Arg = WScript.Arguments
if Arg.Count <> 2 then
 Wscript.Echo ">>Missing parameters"
else
 Wscript.Echo ">>"
 Wscript.Echo ">>"
end if

Function
VB
Public Function RunScript(ByVal args As String) As Script_Struct
    Dim start As New ProcessStartInfo
    start.FileName = "C:\WINDOWS\system32\cscript.exe"
    start.Arguments = args
    start.UseShellExecute = False
    start.RedirectStandardOutput = True
    start.CreateNoWindow = True

    Dim process As New Process
    process.StartInfo = start
    process.Start()
    Dim ret As New Script_Struct
    Dim out As System.IO.StreamReader = process.StandardOutput
    ret.out = out.ReadToEnd
    Try
        Dim err As System.IO.StreamReader = process.StandardError
        ret.err = err.ReadToEnd
    Catch ex As Exception
    End Try
    process.WaitForExit()
    Return ret
End Function

Calls
VB
Dim ret As Script_Struct = RunScript(args)
MsgBox(ret.out)

Structure
VB
Public Structure Script_Struct
    Dim out As String
    Dim err As String
End Structure

Argument (Parameters Pass) as String
"D:\Christian Jay Soyosa\Programs\.NET Desktop\Key Imaging\Configuration\Configuration\bin\Debug\Test.vbs" "1" "2"

Output is
Microsoft (R) Windows Script Host Version 5.8<br />
Copyright (C) Microsoft Corporation. All rights reserved.


I have try creating a .VBS file and running a message box appears. Is the Wscript.Echo not the syntax for this?
Posted
Updated 11-Jan-15 23:38pm
v3
Comments
Sinisa Hajnal 12-Jan-15 5:50am    
What would you expect as an output? You're using MsgBox in the code above, not Echo so appearance of the message box is expected behaviour.

What is the problem?
hansoctantan 13-Jan-15 3:36am    
I would like to get the ">>" as a return in the out.ReadToEnd

1 solution

There are some reasons:

1. VBScript has not any structures, you need to use Class.

2. Using instanses of classes you need to use "Set" like this:

VB
Set Script_Class = RunScript(args)


3. Your syntax is a VBA / VB.NET syntax and NOT a VBScript, e.g. instead of "Return ret" you need

VB
RunScript = ret


and so long ...
 
Share this answer
 
Comments
hansoctantan 13-Jan-15 3:37am    
my problem is not the code but the argument/VbScript itself

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900