?
Solved

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

Posted on 2005-04-04
5
Medium Priority
?
1,887 Views
Last Modified: 2009-10-29
Hi,

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()
        s.WriteLine("Backing site with command: " & sharepoint_site_url)
        s.WriteLine()
        s.WriteLine("Sites to be backed up: " & sharepoint_backup_sites)
        s.WriteLine()
        s.WriteLine()

        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)

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

    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" />
</Sites>

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.

Thanks,

Dave
0
Comment
Question by:FPIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 2000 total points
ID: 13700632
From here:
How do I launch and monitor external programs from .NET?
http://www.thescarms.com/dotNet/Process.asp

'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
        oProcess.Start()

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

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

Author Comment

by:FPIT
ID: 13700637
Cool...i'll try it.   Thanks
0
 
LVL 2

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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