Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use psexec on a remote machine and display command window?

Posted on 2010-08-22
13
Medium Priority
?
975 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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