Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

use psexec on a remote machine and display command window?

Posted on 2010-08-22
13
Medium Priority
?
981 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

916 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