Solved

use psexec on a remote machine and display command window?

Posted on 2010-08-22
13
941 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridView 2 29
Problem to Popup 2 53
How do I get the id from URL? 19 48
Copy/Clone an object. 9 18
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

8 Experts available now in Live!

Get 1:1 Help Now