Solved

use psexec on a remote machine and display command window?

Posted on 2010-08-22
13
960 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
[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
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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