使用管理员权限运行 cmd.exe [英] Running cmd.exe with admin priviliges

查看:53
本文介绍了使用管理员权限运行 cmd.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试以管理员权限运行 cmd.exe 的代码.但是,我得到请求操作需要提升.如果我通过我的 Windows 使用以管理员身份运行"运行 cmd.exe,它会工作,但是,通过 vb,它不会.这是我的代码.

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)

谢谢.

推荐答案

你无法实现你想要的.

可以使用Process.Start()来启动一个提升的进程,但只有如果你UseShellExecute = true代码>:

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()

原因是因为如果您想启动提升的进程,您必须使用 ShellExecute.只有 ShellExecute 知道如何提升.

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

如果您指定 UseShellExecute = False,则使用 CreateProcess 而不是 ShellExecute.CreateProcess 不知道如何提升.为什么?来自 AppCompat 人员:

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

嗯,CreateProcess 在层中真的很低.如果没有创建流程的能力,你能做什么?不是很多.然而,海拔是另一回事.它需要访问应用程序提升服务.然后调用consent.exe,它必须知道如何读取组策略,并在必要时切换到安全桌面并弹出一个窗口并要求用户提供权限/凭据等.我们甚至不需要获取所有这些功能,让我们只获取对话框.

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.

现在,要创建需要提升的流程,通常您只需切换 API.外壳位于更高的层中,因此能够依赖于海拔.因此,您只需将您对 CreateProcess 的调用替换为对 ShellExecute 的调用.

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.

这就解释了如何提升cmd,但是一旦你这样做了:你不能重定向输出,或者隐藏窗口;因为只有 CreateProcess 可以做到这一点:

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:

重定向 I/O 和隐藏窗口只有在进程由 CreateProcess() 启动时才能起作用.

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.

这篇关于使用管理员权限运行 cmd.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆