0

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)
Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
Adam
  • 1
  • 1
  • Some options discussed here: https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically – Jon Roberts Jul 17 '19 at 13:45

1 Answers1

0

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)
RobertBaron
  • 2,817
  • 1
  • 12
  • 19
  • That just opens the command window, how do I run commands through it – Adam Jul 17 '19 at 14:18
  • The window doesn't stay open, it just flashes open and then immediately closes – Adam Jul 17 '19 at 14:58
  • @Adam - That is normal if the `permanent` argument is set to `False`. For the window to stay opened, the `permanent` argument has to be set to `True`. This is how you have written your `runCmd` `Sub`. – RobertBaron Jul 17 '19 at 15:01