How to start an external program using VBScript.

I want to write a VBscript that runs the program DCDIAG.EXE with switches and outputs the results to a text file.
I want it to do the below equivalent.


What I have so far is below but it doesn't run correctly. It runs the DCDIAG but not with the switches etc.

strComputer = "MyComputer"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
RunResult = objWMIService.Create "DCDIAG.EXE /S:MYSERVER /Q  C:\DCDIAG.LOG",null,null )

Who is Participating?
That's because of a problem with the way console apps are run from within other processes.  See:

HOWTO: Redirect Standard I/O of a Console App Using Batch File (Q150767);en-us;Q150767

Here is the standard MS workaround outlined in that link, redesigned for VBScript.

Set oWShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = oFSO.CreateTextFile("C:\temp.bat")
oTextStream.Write "Dir C:\*.* > C:\Directory.txt"

oWShell.Run "c:\temp.bat", , True
oFSO.DeleteFile "C:\temp.bat"
Is there a permissions problem or something?

Set WShell = CreateObject("WScript.Shell")  WShell.Run "c:\DCDIAG.EXE /S:MYSERVER /Q >C:\DCDIAG.LOG"
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

petermohAuthor Commented:
PaulHews, your answer does run

Set WShell = CreateObject("WScript.Shell")  WShell.Run "c:\DCDIAG.EXE /S:MYSERVER /Q >C:\DCDIAG.LOG"

with the switchs /s and /q however it doesn't output the results to a text file. i.e. the >C:\DCDIAG.LOG doesn't work. I need this bit working. Permissions are not a problem I just have very limited knowledge of VBScript. FYI it you want to test just substitute DCDIAG with another simple utility that outputs data like tlist for example.

I used Dir, but you can use any console app that outputs to the StdOut.
petermohAuthor Commented:
Thanks Paul.
Just stumbled across this while looking for a creative way to do some scripting and thought I would update that 7 years later now you can run DCDIAG /f:text.log or /ferr:text.log to log only errors.  No batch necessary.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.