Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running cmd.exe with admin priviliges

Tags:

vb.net

This is my code trying to run cmd.exe with admin priviligies. However, I get the request operation requires elevation. if I run cmd.exe with "Run as Admin" through my windows, it works, however, through vb, it doesn't. This is my code.

Try
        Dim process As New Process()
        process.StartInfo.FileName = "cmd.exe "
        process.StartInfo.Verb = "runas"
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardInput = True
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.RedirectStandardError = True
        process.StartInfo.CreateNoWindow = True

        process.Start()
        process.StandardInput.WriteLine("route add 8.31.99.141 mask 255.255.255.255 " & cmdorder)
        process.StandardInput.WriteLine("exit")
        Dim input As String = process.StandardOutput.ReadToEnd
        process.Close()
        Dim regex As Regex = New Regex("(ok)+", RegexOptions.IgnoreCase) ' wa requested
        ' txtLog.AppendText(input)
        Return regex.IsMatch(input)

Thanks.

like image 866
user2550788 Avatar asked Jul 24 '13 19:07

user2550788


1 Answers

You cannot achieve what you want.

You can use Process.Start() to launch an elevated process, but only if you UseShellExecute = true:

Dim process As New Process()
process.StartInfo.FileName = "cmd.exe "
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = True
process.Start()

The reason is because you must use ShellExecute if you want to launch an elevated process. Only ShellExecute knows how to elevate.

If you specify UseShellExecute = False, then CreateProcess is used rather than ShellExecute. CreateProcess doesn't know how to elevate. Why? From the AppCompat guy:

Well, CreateProcess is really low in the layers. What can you do without the ability to create a process? Not a whole lot. Elevation, however, is a different story. It requires a trip to the app elevation service. This then calls into consent.exe, which has to know how to read group policy and, if necessary, switch to the secure desktop and pop open a window and ask the user for permission / credentials, etc. We don’t even need to take all of these features, let’s just take the dialog box.

Now, for creating a process that requires elevation, normally you just switch up APIs. The shell sits in a much higher layer, and consequently is able to take a dependency on elevation. So, you’d just swap out your call to CreateProcess with a call to ShellExecute.

So that explains how you can elevate cmd, but once you do: you're not allowed to redirect output, or hide the window; as only CreateProcess can do that:

Redirecting I/O and hiding the window can only work if the process is started by CreateProcess().

Which was a long way of saying that this guy asked the same question over here; but without the indignity of having someone close your question.

Note: Any code is released into the public domain. No attribution required.

like image 54
Ian Boyd Avatar answered Oct 29 '22 13:10

Ian Boyd