RunAs

V

Vittorio Pavesi

Hello,
I'm trying to execute RunAs but I'm experiencing problem with standardinput;
it seems that the process doesn't receive it.
Here is the code, any help ?

Public Sub ActivateRunAs(ByVal AppPath As String, ByVal Username As
String, ByVal Password As String)
Dim RunAsPath As String =
System.Environment.GetEnvironmentVariable("SystemRoot") &
"\system32\runas.exe"
Dim Arg = " /user:" & Username & " " & AppPath & " "
Dim InputString As String
Dim p As New Process
p.StartInfo.FileName = RunAsPath
p.EnableRaisingEvents = True
p.StartInfo.Arguments = Arg
p.StartInfo.UseShellExecute = False
p.Start()
p.StartInfo.RedirectStandardInput = True
p.StandardInput.WriteLine(Password)
End Sub

I also tried with the following code, but it doesn't work.
p.StandardInput.BaseStream.Write(System.Text.Encoding.Unicode.GetBytes(Passw
ord), 0, Password.Length)
 
C

Cor

Hi Vittorio,

Maybe I can help you when you tell what you want to do, but doing it using
the standard input is luckily since the C/PM time not my stuff anymore.

Cor
 
V

Vittorio Pavesi

I need to create a WinApp that use the command line version of runas.exe
with cabled the username and the password.
I know that it's not really secure but it's really better than use a
vbscript or add many users to the administrators group (also this is the
customer requirement and I can't change it).
When you use: runas.exe /user:administrator notepad.exe it will ask you to
input the password (it's not allowed to give it as an argument) so I need to
use standardinput to write the password but it seems that my code doesn't
work.
I hope everything is clear and I can get help

Vittorio
 
C

Cor

Hi Vittorio,

It is not my stuff, but this should be near it.
You can try it (I have nothing to test this).

Cor
p.StartInfo.Arguments = Arg
p.StartInfo.UseShellExecute = False
p.Start()
p.StartInfo.RedirectStandardInput = True
Dim sw As io.StreamWriter = myProcess.StandardInput
Dim inputText As String
Console.WriteLine("Enter a pasword:")
inputText = Console.ReadLine()
sw.WriteLine(inputText)
 
V

Vittorio Pavesi

Hi Cor,
it seems that runas.exe works in a different way than cmd.exe
This works fine:

Public Sub test1()
Dim myProcess As New Process
Dim myProcessStartInfo As New ProcessStartInfo("cmd.exe")
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcessStartInfo.RedirectStandardInput = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()
myProcess.StandardInput.WriteLine("dir C:\ > c:\test.txt")
myProcess.Close()
End Sub

But it doesnt' work with runas.exe

Vittorio
 
C

Cor

Hi Vittorio,

did you try my sample.

I now see there is a little error in it?

I took some investigation, but did it getting the standard output, because I
had no program with standard input. But I thought this was even simpler.

Cor
 

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