Adam
Adam

Reputation: 1

Writing to a newly created command prompt window using vb.net

I'm trying to use command prompt as an admin using vb.net and I'm opening it by using runas in the default cmd.exe file. I want to then run commands through the newly opened command prompt window running as the domain admin using vb.net. How do I go about doing this?

This is the method that I'm using:

Public Sub runCmd(ByVal pass As String, ByVal command As String, ByVal arguments As String, ByVal permanent As Boolean)
    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    pi.Arguments = " "+ If(permanent = True, "/K", "/C") + " " + command + " " + arguments
    pi.FileName = "cmd.exe"
    p.StartInfo = pi
    p.Start()
End Sub

This is the call that opens cmd:

runCmd(strPass, "runas", "/user:<domain>\" + strUser + " cmd", False)

Upvotes: 0

Views: 139

Answers (1)

RobertBaron
RobertBaron

Reputation: 2854

You need to set the Verb property of the StartInfo to "runas".

Public Sub runCmd(ByVal pass As String, ByVal command As String, ByVal arguments As String, ByVal permanent As Boolean)
    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
    pi.FileName = "cmd.exe"
    pi.Verb = "runas"
    p.StartInfo = pi
    p.Start()
End Sub

And you then call your function like this:

runCmd(strPass, "", "/user:<domain>\" + strUser, False)

Upvotes: 0

Related Questions