How To Handle RUNAS Password Using The Process Object

P

Phillip Galey

I'm trying to use the Process object to have RUNAS run DOS commands. I have
no problem getting the Process object to run DOS commands and return the
resulting text back to the program. However, if I'm using RUNAS (e.g. in
order to move files to a protected network share), I run into the problem
that RUNAS requires a two-step process to get it started.

If you're calling RUNAS from the command prompt, after entering the command
line text and hitting ENTER, you're then asked for the password. RUNAS's
command line does now allow you to include the password, so it's a two-step
process.

I've been trying to work with objMyProcess.RedirectStandardInput and
objMyProcess.StandardInput to feed it the password, but nothing seems to be
working.

The following is my code. The MsgBox line returns "Enter password for
MyDomain\MyUsername:RUNAS ERROR: Unable to change echo mode"
Dim p As New Process()
Dim psi As New ProcessStartInfo("C:\WINNT\system32\runas.exe", _
"/profile /user:MyDomain\MyUsername " & """" & "c:\winnt\system32\xcopy.exe
/V /Y C:\MyFile.txt \\MyServer\MyShare\SomeFolder" & """")

psi.UseShellExecute = False
psi.RedirectStandardInput = True
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.CreateNoWindow = True
p.StartInfo = psi
p.Start()
p.StandardInput.AutoFlush = True
p.StandardInput.WriteLine("MyPassword")
MsgBox(p.StandardOutput.ReadToEnd)
p.StandardInput.Flush()
p.StandardInput.Close()
p.StandardOutput.Close()
p.Close()
p = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top