Solved

use psexec on a remote machine and display command window?

Posted on 2010-08-22
13
943 Views
Last Modified: 2013-11-07
I run a command with psexec, but I need to keep the command window open, or redirect the output to a variable.  Here is the code I have, what can I do to make that happen?

 Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click
        '*********sys info button********************************************************************
        If PingCheck() = True Then
            PressEnter(4) 'make we see workstation selected


            If ValidateActiveDirectoryLogin("domain", strusername, strUserPassword) Then
                'password is good, contine

            Else
                'bad password
                MessageBox.Show("Your password is not correct.  Please enter a new password.", "Enter Correct Password", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox7.Enabled = True
                TextBox7.Text = ""

                'disable buttons
                Button23.Enabled = False
                Button14.Enabled = False
                Exit Sub

            End If
           

            Dim RebuildProcess As New ProcessStartInfo(scriptlocation & "psexec")   'setup process and define where psexec is located
            RebuildProcess.Arguments = "\\" & strWorkstationFQDN & " -u domain\" & strusername & " -p " & strUserPassword & " systeminfo"   'give the arguments for psexec
            Process.Start(RebuildProcess)   'run psexec

        Else
            MessageBox.Show("You must be able to ping the workstation to use syteminfo.", "Can't Ping Workstation", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
0
Comment
Question by:chadmanvb
  • 7
  • 6
13 Comments
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33497953
Hello !

here's a bit of code that might do what you need... with a little modification.

Syldra
Dim strOut as string

Dim strErr a string



Private Sub CMD()

        Dim procCMD As New Diagnostics.Process

        With procCMD.StartInfo

            .CreateNoWindow = True

            .RedirectStandardOutput = True

            .RedirectStandardError = True

            .UseShellExecute = False

            .FileName = "cmd.exe"

            .Arguments = "Psexec.exe \\" & strComputerName & " cmd.exe"

        End With

        AddHandler procCMD.OutputDataReceived, AddressOf procCMD_OutputDataRecieved

        AddHandler procCMD.ErrorDataReceived, AddressOf procCMD_ErrorDataRecieved

        procCMD.Start()

        procCMD.BeginOutputReadLine()

    End Sub



    Private Sub procCMD_OutputDataRecieved(ByVal sender As Object, ByVal output As DataReceivedEventArgs)

        strOut += output.Data

    End Sub



    Private Sub procCMD_ErrorDataRecieved(ByVal sender As Object, ByVal output As DataReceivedEventArgs)

        strErr(output.Data)

    End Sub

Open in new window

0
 

Author Comment

by:chadmanvb
ID: 33497976
I just tried this, but it does not seem to work.  I do not get any output
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33498046
Ok, there's an error on line 25, it should read "strErr += output.data". Sorry.

Put a spy on strOut and strErr... do they get updated ?

I'll check my code again, there may be something elsewhere that's needed.

Syldra
0
 

Author Comment

by:chadmanvb
ID: 33498344
Also note that the psexec is located at a unc path.  I was have to have a username/password when running this command.  It does work fine when I run it from a command line.  I would just like to run it within my app and caputre the data.
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33500607
Hello !

here's your code modified to handle output redirection. Your data should be in the strOut and strErr variables. Just put a break on "strOut +=..." and "strErr +=..." to see if they are triggered.

Syldra
Dim strOut as string

Dim strErr a string



Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click

        '*********sys info button********************************************************************

        If PingCheck() = True Then

            PressEnter(4) 'make we see workstation selected



            If ValidateActiveDirectoryLogin("domain", strusername, strUserPassword) Then

                'password is good, contine



            Else

                'bad password

                MessageBox.Show("Your password is not correct.  Please enter a new password.", "Enter Correct Password", MessageBoxButtons.OK, MessageBoxIcon.Error)

                TextBox7.Enabled = True

                TextBox7.Text = ""



                'disable buttons

                Button23.Enabled = False

                Button14.Enabled = False

                Exit Sub



            End If

            



            Dim RebuildProcess As New ProcessStartInfo(scriptlocation & "psexec")   'setup process and define where psexec is located

            With RebuildProcess

                 .CreateNoWindow = True

                 .RedirectStandardOutput = True

                 .RedirectStandardError = True

                 .UseShellExecute = False

                 .Arguments = "\\" & strWorkstationFQDN & " -u domain\" & strusername & " -p " & strUserPassword & " systeminfo"   'give the arguments for psexec

            End With

            AddHandler RebuildProcess.OutputDataReceived, AddressOf RebuildProcess_OutputDataRecieved

            AddHandler RebuildProcess.ErrorDataReceived, AddressOf RebuildProcess_ErrorDataRecieved

            Process.Start(RebuildProcess)   'run psexec

            RebuildProcess.BeginOutputReadLine()

        Else

            MessageBox.Show("You must be able to ping the workstation to use syteminfo.", "Can't Ping Workstation", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Sub 



    Private Sub RebuildProcess_OutputDataRecieved(ByVal sender As Object, ByVal output As DataReceivedEventArgs)

        strOut += output.Data

    End Sub



    Private Sub RebuildProcess_ErrorDataRecieved(ByVal sender As Object, ByVal output As DataReceivedEventArgs)

        strErr += output.Data

    End Sub

Open in new window

0
 

Author Comment

by:chadmanvb
ID: 33500978
Thank you, but I am not able to compile this:

  AddHandler RebuildProcess.OutputDataReceive, AddressOf RebuildProcess_OutputDataRecieved
                AddHandler RebuildProcess.ErrorDataReceived, AddressOf RebuildProcess_ErrorDataRecieved
                Process.Start(RebuildProcess)   'run psexec
                RebuildProcess.BeginOutputReadLine()

OutputDataReceive and ErrorDataReceived and BeginOutputReadLine are not part of the 'System.Diagnostics.ProcessStartInfo'.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33501062
Ok, here's the part you need to change.

Syldra
Dim RebuildProcess As New Diagnostic.Process

             With RebuildProcess 

                 .CreateNoWindow = True 

                 .RedirectStandardOutput = True 

                 .RedirectStandardError = True 

                 .UseShellExecute = False 

                 .Filename = scriptlocation & "psexec"   'setup process and define where psexec is located 

                 .Arguments = "\\" & strWorkstationFQDN & " -u domain\" & strusername & " -p " & strUserPassword & " systeminfo"   'give the arguments for psexec 

            End With 

            AddHandler RebuildProcess.OutputDataReceived, AddressOf RebuildProcess_OutputDataRecieved 

            AddHandler RebuildProcess.ErrorDataReceived, AddressOf RebuildProcess_ErrorDataRecieved 

            RebuildProcess.Start  'run psexec 

            RebuildProcess.BeginOutputReadLine()

Open in new window

0
 

Author Comment

by:chadmanvb
ID: 33507345
I am not seeing strOut += or strErr +=  getting any data.  I do the command window come up on my screen and run the command fine.  Am I still missing something?
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33510898
On line 2, change "With RebuildProcess" by "With RebuildProcess.StartInfo".

I hope it does the trick because I can't think of any reason why it would work for me and not for you.

Syldra
0
 

Author Comment

by:chadmanvb
ID: 33517171
are you able to run the systeminfo command?  If I change it to something like ipconfig it works fine.  I commented out the option to see the cmd window.  When I try to run system info it looks like it opens a cmd window and then opens another cmd window to run systeminfo.  I think that is why I am not seeing anything.  The first command window just stays blank.  any ideas?
0
 
LVL 8

Accepted Solution

by:
SylvainDrapeau earned 500 total points
ID: 33517833
I have an idea ! Don't know why I did not think about it earlier...

The systeminfo tool can be run on a remote computer without using psexec.

Change this :
.Filename = scriptlocation & "psexec"
.Arguments = "\\" & strWorkstationFQDN & " -u domain\" & strusername & " -p " & strUserPassword & " systeminfo"

for this :
.Filename = "cmd.exe"
.Arguments = "/c systeminfo /s " & strWorkstationFQDN & " /u domain\" & strusername & " /p " & strUserPassword

Now THIS will work, garanteed.

Syldra
0
 

Author Closing Comment

by:chadmanvb
ID: 33525306
Got it!  Thanks so much for your time and help!
0
 
LVL 8

Expert Comment

by:SylvainDrapeau
ID: 33526145
Great news !

Thank you !

Syldra
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now