Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have seen many questions and answers on this subject, however, I can't seem to find the right answer for my situation. I have code that works perfectly as long as I don't set the username, password and domain of the processStartInfo object.

VB
Dim p As New ProcessStartInfo()
            With p
                Dim exec As String = "C:\Program Files\MyFiles\PsExec.exe"
                If File.Exists(exec) Then
                    p.FileName = exec
                    p.Arguments = "\\" & env & " -u username -p password -d icpcl -source """ & source & """ -script " & script
                    p.WindowStyle = ProcessWindowStyle.Hidden
                    p.UseShellExecute = False
                    p.RedirectStandardOutput = True
                    p.RedirectStandardError = True
                    'p.UserName = "myUser"
                    'p.Password = ConvertToSecureString("myPassword")
                    'p.Domain = "myDomain"

                    Dim x As Process = Process.Start(p)
                    Dim output As String = x.StandardOutput.ReadToEnd()
                    Dim err As String = x.StandardError.ReadToEnd()
                    WriteLog("output: " & output)
                    WriteLog("err: " & err)
                    x.WaitForExit()
                End If
            End With


If I uncomment the UserName, Password and Domain lines, I get the following error:

"The directory name is invalid"

If I change

VB
p.FileName = exec


to

VB
p.WorkingDirectory = "C:\Program Files\MyFiles\"
p.FileName = "PsExec.exe"


Then I get a different error:

"The system cannot find the path specified"

Both ways work just fine if I don't use the UserName, Password and Domain.

I don't understand what I'm missing here. Any help is greatly appreciated.

Thank you in advance,
t shaffer
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 16:24pm    
This is unrelated to programming. This is related to the use of one particular program PsExec (is it Sysinternals telnet replacement)? It does work...
—SA
Tisha Shaffer 4-Mar-14 16:47pm    
I don't understand what you are saying. Yes, it does work perfectly, if I don't assign the UserName, Password and Domain and run the program as myself on my developement PC. If I assign my own credentials to UserName, Password and Domain, I get the errors.
I'm having trouble seeing how your comment helps me.

t shaffer
Bernhard Hiller 5-Mar-14 3:20am    
More likely, the problem is in that "PsExec.exe" which is unknown to any of us here - it is in C:\Program Files\MyFiles which indicates that it is your work.
I assume that the Impersonization happens in that program - your code snippet does not show anything related to impersonization at all. Show us the relevant code snippets of PsExec.

1 solution

Maybe Impersonization is the wrong word, I need to run a process on the local PC that the program runs on as a different user than the user that is logged on. From what I understand, when using ProcessStartInfo, setting the UserName, Password and Domain will accomplish this task.
I simplified my code to simply run notepad.exe. I copied notepad.exe into "MyFiles" directory.
Remember, I'm setting the UserName, Password and Domain to my own credentials. I still get the same results:
VB
Dim p As New ProcessStartInfo()
            With p
                Dim exec As String = "C:\Program Files\MyFiles\notepad.exe"
                If File.Exists(exec) Then
                    p.FileName = exec
                    p.WindowStyle = ProcessWindowStyle.Hidden
                    p.UseShellExecute = False
                    p.RedirectStandardOutput = True
                    p.RedirectStandardError = True
                    'p.UserName = "myUser"
                    'p.Password = ConvertToSecureString("myPassword")
                    'p.Domain = "myDomain"

                    Dim x As Process = Process.Start(p)
                    Dim output As String = x.StandardOutput.ReadToEnd()
                    Dim err As String = x.StandardError.ReadToEnd()
                    WriteLog("output: " & output)
                    WriteLog("err: " & err)
                    x.WaitForExit()
                End If
            End With


In my opinion, this has nothing to do with what process I am trying to run.

Thank you,
tshaffer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900