News, Product Information, and Tips
Check out our free utlities in the downloads section

Remote Administration



Remote Administration For Windows. Easy remote access of Windows 7, XP, 2008, 2000, and Vista Computers

Click here to find out more

Create Outlook Profiles



No more setup wizards in Outlook. Setup Outlook Profiles automatically from the command line

Click here to find out more

Network Administrator



Reboot Hundreds of computers, disable flash drives, deploy power managements settings.

Click here to get your free copy of Network Administrator. Over 25 plugins to make your life easier

USB Disabler



Disable Flash Drives for specific users, or only allow specific drives.

Click here for your free trial

Search

Archives

How to run an external program within VB Script

by Steve Wiseman on January 2, 2007 · 2 comments

in Windows

When VB Script executes a command line program it opens a new window. This can be especially inconvenient when you want to incorporate an external application into your script.

Console Windows

There is a way to have your script keep the console application within the same window, and wait for it to complete.

What we will do is launch the process, and then forward all of the console output of the script. When the process has terminated we exit the loop.

Here is a function that you can drop into your script (Click here to download)

Function ExecuteShellProgram(ByVal sFileName)

Dim poShell
Dim poProcess
Dim iStatus

Set poShell = CreateObject(“WScript.Shell”)
Set poProcess = poShell.Exec(sFileName)

‘Check to see if we started the process without error

if ((poProcess.ProcessID=0) and (poProcess.Status=1)) then
Err.Raise vbObjectError,,”Failed executing process”
end if

‘Now loop until the process has terminated, and pull out
‘any console output

Do
‘Get current state of the process
iStatus = poProcess.Status

‘Forward console output from launched process
‘to ours
WScript.StdOut.Write poProcess.StdOut.ReadAll()
WScript.StdErr.Write poProcess.StdErr.ReadAll()

‘Did the process terminate?
if (iStatus 0) then
Exit Do
end if
Loop

‘Return the exit code
ExecuteShellProgram = poProcess.ExitCode

End Function

Here is a simple example. I am going to call the command interpreter, and do listing of the windows folder:


ExecuteShellProgram “%COMSPEC% /c dir c:\windows”

(%COMSPEC% is a system variable that is set to the full path and file name of the command interpreter cmd.exe)

Now you can execute your console helper applications – and use the output in your script.

Like this article? Then sign up for my newsletter to get free tips and software sent right to your inbox once a week. Like you, I hate spam – I will never spam, or sell your email address.

Related Articles:

{ 2 comments… read them below or add one }

1 tsohr November 5, 2009 at 12:44 am

Thanks!! You save a lot of my time.
It’s quite interesting that the WScript.Shell object have the stdin and stdout pipe default.
Very good.

2 Mazhar November 29, 2010 at 5:57 am

Some one help me
I want to make a batch file which runs a task on server and this batch file will be run from a computer on network i am not aware of scripts and batch any one please give me code of this that how to run the task in task schedular.
I am using windows server 2008R2
Thanks

Leave a Comment

Category Links - Windows Forum - Exchange Forum