שתף באמצעות


Running multiple commands in one process

Question

Friday, April 3, 2009 11:12 PM | 2 votes

Hi all
I understand running the command shell as a process, How would i run multiple arguments in that one process? For example, here's a process to do a netstat.

Dim pNet As New Process
        pNet.StartInfo.FileName = "cmd.exe"
        pNet.StartInfo.Arguments = " /k netstat -r "
        Try
            pNet.Start()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

How would I do that with the following commands? I'd like to click one button and have these all run one after the other.

MKDIR Logs
Dcdiag.exe /e > .\Logs\dcdiag.txt
Netdiag.exe /v > .\Logs\Netdiag.txt
Repadmin /showreps > .\Logs\rep_partners.txt
Repadmin /replsum /errorsonly > .\Logs\repadmin_err.txt

All replies (9)

Saturday, April 4, 2009 12:31 AM ✅Answered | 2 votes

Using p1 As New Process
    p1.StartInfo.FileName = "cmd.exe"
    p1.StartInfo.Arguments = "/k ..."
    p1.WaitForExit()
End Using

Using p2 As New Process
    p2.StartInfo.FileName = "cmd.exe"
    p2.StartInfo.Arguments = "/k ..."
    p2.WaitForExit()
End Using

Using p3 As New Process
    p3.StartInfo.FileName = "cmd.exe"
    p3.StartInfo.Arguments = "/k ..."
    p3.WaitForExit()
End Using

'Etc.

Friday, April 3, 2009 11:21 PM | 1 vote

cmd.exe understands the && operator to specify multiple commands so that the second is executed if the first succeeds (errorlevel 0).  The || operator can be used if you want the execution to happen unconditionally (i.e., without looking at the error number)

However, I would not bother.  Why not just call Process.Start for each one?  You can use the Process.WaitForExit method if you do not want them to execute concurrently.


Friday, April 3, 2009 11:28 PM | 1 vote

Thanks BC,
Can you give me an example? Something like this?

Dim pMulti As New Process
        pMulti.StartInfo.FileName = "cmd.exe"
        pMulti.StartInfo.Arguments = " /k MKDIR Logs"
        pMulti.StartInfo.Arguments = "Dcdiag.exe /e > .\Logs\dcdiag.txt"
        pMulti.StartInfo.Arguments = "Netdiag.exe /v > .\Logs\Netdiag.txt"
        pMulti.StartInfo.Arguments = "Repadmin /showreps > .\Logs\rep_partners.txt"
        pMulti.StartInfo.Arguments = "Repadmin /replsum /errorsonly > .\Logs\repadmin_err.txt"""
        pMulti.Start()

Saturday, April 4, 2009 1:03 AM | 1 vote

Thanks BC, I get this error "no process is associated with this object." when I run

Using p1 As New Process
            p1.StartInfo.FileName = "cmd.exe"
            p1.StartInfo.Arguments = " /k MKDIR .\Logs"
            p1.WaitForExit()
End Using

Saturday, April 4, 2009 1:05 AM | 1 vote

Thanks BC, I get the error "no process is associated with this object" on the waitforexit when i run the code below.

        Using p1 As New Process
            p1.StartInfo.FileName = "cmd.exe"
            p1.StartInfo.Arguments = " /k MKDIR .\Logs"
            p1.WaitForExit()
        End Using

Saturday, April 4, 2009 1:29 AM | 1 vote

Ok, actually I've done this and it seems to work. How would I get the cmd window to close after the command runs?

        Using p1 As New Process
            p1.StartInfo.FileName = "cmd.exe"
            p1.StartInfo.Arguments = " /k MKDIR Logs"
            Try
                p1.Start()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            p1.WaitForExit()
        End Using

        Using p2 As New Process
            p2.StartInfo.FileName = "cmd.exe"
            p2.StartInfo.Arguments = " /k Dcdiag.exe /e > .\Logs\dcdiag.txt"
            Try
                p2.Start()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            p2.WaitForExit()
        End Using

        Using p3 As New Process
            p3.StartInfo.FileName = "cmd.exe"
            p3.StartInfo.Arguments = " /k Netdiag.exe /v > .\Logs\Netdiag.txt"
            Try
                p3.Start()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            p3.WaitForExit()
        End Using

        Using p4 As New Process
            p4.StartInfo.FileName = "cmd.exe"
            p4.StartInfo.Arguments = " /k Repadmin /showreps > .\Logs\rep_partners.txt"
            Try
                p4.Start()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            p4.WaitForExit()
        End Using

        Using p5 As New Process
            p5.StartInfo.FileName = "cmd.exe"
            p5.StartInfo.Arguments = " /k Repadmin /replsum /errorsonly > .\Logs\repadmin_err.txt"
            Try
                p5.Start()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            p5.WaitForExit()
        End Using
    End Sub

Wednesday, May 20, 2009 2:25 PM

Using /c in place of /k terminates the window.


Wednesday, January 10, 2018 4:15 PM

Hello, 

Jason's answer solves it by opening 3 cmd process.. Is there a way to run it using single cmd process.. what if the command lines are interrelated.. 

I am facing one such issue and dont want to use Batch or a ps1 because that can be opened by anyone who has access -- security issue.. 

Mine is something like below: 

if (Test-Path D:\Collect-ServerInfo-1.0.1\PClass.txt) 
{
  Remove-Item D:\Collect-ServerInfo-1.0.1\PClass.txt
  add-item D:\Collect-ServerInfo-1.0.1\PClass.key
}
if (Test-Path D:\Collect-ServerInfo-1.0.1\UClass.txt) 
{
  Remove-Item D:\Collect-ServerInfo-1.0.1\UClass.txt
  add-item D:\Collect-ServerInfo-1.0.1\UClass.txt
}
$username = Set-Content "D:\Collect-ServerInfo-1.0.1\UClass.txt"

regards,

Jaideep

regards,

Jaideep


Wednesday, January 10, 2018 4:32 PM

Don't hack some other ones thread. 

Create your own one by asking a new question. 

It is free

Success Cor