How do I capture output from command prompt in vb.net?

Posted on 2005-04-04
Medium Priority
Last Modified: 2009-10-29

I am creating a vb.net script that will basically backup our Sharepoint Sites and Databases using both the STSADM and SMIGRATE command line tools.  I am also creating a log file to store information about the backup each day that it runs.  So, i need to know how i can use the Shell() function or some function to run the above mentioned command line tools and capture the output that is displayed on the dos screen.  Any help around this topic would be greatly appreciated...here's some of my code to show you where i'm at with it at the moment:

    Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        
        Dim fileStream As StreamReader
        Dim str_cmd, cmd_smigrate, cmd_smigrate_output_path, cmd_stsadm, cmd_stsadm_output_path As String
        Dim todaysDate, fileToExport, exportFolder, output, sharepoint_site_url, sharepoint_backup_sites As String
        Dim res_stsadm, res_smigrate As Integer

        todaysDate = Date.Today.Month.ToString & "_" & Date.Today.Day.ToString & "_" & Date.Today.Year.ToString
        fileToExport = "sharepoint_" & todaysDate & ".txt"

        exportFolder = "F:\Program Files\SharePoint Portal Server\Backup\Logs\" & fileToExport
        sharepoint_site_url = "stsadm -o enumsites -url http://sql-prod"
        sharepoint_backup_sites = Shell(sharepoint_site_url, AppWinStyle.NormalFocus)

        Dim fs As New FileStream(exportFolder, FileMode.Create, FileAccess.Write)
        Dim s As New StreamWriter(fs)
        s.BaseStream.Seek(0, SeekOrigin.End)
        s.WriteLine("Starting backup!")
        s.WriteLine("Backing site with command: " & sharepoint_site_url)
        s.WriteLine("Sites to be backed up: " & sharepoint_backup_sites)

        cmd_stsadm_output_path = "F:\Program Files\SharePoint Portal Server\Backup\sharepoint_sites.dat"
        cmd_stsadm = "stsadm -o backup -url http://sql-prod -filename " & cmd_stsadm_output_path & " - overwrite"
        res_stsadm = Shell(cmd_stsadm, AppWinStyle.NormalFocus, True, -1)

        cmd_smigrate_output_path = "C:\sql_prod_backup"
        cmd_smigrate = "smigrate -w http://sql-prod -f " & cmd_smigrate_output_path
        res_smigrate = Shell(cmd_smigrate, AppWinStyle.NormalFocus)

        s.WriteLine(vbCrLf & vbCrLf & "Backup completed successfully!")

    End Sub

As of right now the commands are running correctly to create my backup files but if you look at the following line of code:

sharepoint_backup_sites = Shell(sharepoint_site_url, AppWinStyle.NormalFocus)

...it returns some crazy number like 2992 or something...When the output on the command line screen is the following:

<Sites Count="2">
  <Site Url="http://sql-prod" Owner="GROUPSYNC\dbrinston" />
  <Site Url="http://sql-prod/personal/dbrinston" Owner="GROUPSYNC\dbrinston" />

So the above output from the command line is what i want to capture so that i can enter it in my log file.  Again any help would be appreciated.


Question by:FPIT
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13700632
From here:
How do I launch and monitor external programs from .NET?

'The following demonstrates how to run a BAT file and redirect the output to your program
        Dim oProcess As New Process
        'this is the bat file to run
        Dim s As String = "c:\q.bat"
        Dim sOutput As String

        oProcess.StartInfo.FileName = s
        oProcess.StartInfo.UseShellExecute = False
        'oProcess.StartInfo.CreateNoWindow = True
        oProcess.StartInfo.RedirectStandardOutput = True

        Dim sOut As System.IO.StreamReader = oProcess.StandardOutput

        oProcess.WaitForExit(10000) '10 seconds
        'grab the StdOut
        sOutput = sOut.ReadToEnd
        'clean up
        'display the results

Author Comment

ID: 13700637
Cool...i'll try it.   Thanks

Expert Comment

ID: 24094681
Than you Erick37, this is exactly what I was looking for.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question