简体   繁体   English

使用 -Verb RunAs 通过 Start-Process 运行 cmd.exe,然后以管理员身份自动运行命令

[英]Running cmd.exe through Start-Process with -Verb RunAs and then automatically running a command as Administrator

Starting from a non-elevated PowerShell, I want to be able to launch a command prompt as Administrator and then run a particular tool as Administrator, ultimately to be used for a right-click menu option.从非提升的 PowerShell 开始,我希望能够以管理员身份启动命令提示符,然后以管理员身份运行特定工具,最终用于右键单击菜单选项。

I have tried this so far:到目前为止,我已经尝试过:

Start-Process cmd -ArgumentList '/k tool.exe' -Verb RunAs

While this does launch a command prompt as Administrator, it runs tool.exe first in a non-elevated command prompt and then switches to an Administrator command prompt.虽然这确实以管理员身份启动命令提示符,但它首先在非提升命令提示符中运行 tool.exe,然后切换到管理员命令提示符。 How can I get this to run tool.exe as Administrator?我怎样才能让它以管理员身份运行 tool.exe?

Edit: As @mklement0 pointed out, the command prompt actually was being run as Administrator, it just wasn't reflected immediately in the window title (possibly a bug?).编辑:正如@mklement0 指出的那样,命令提示符实际上是以管理员身份运行的,只是没有立即反映在 window 标题中(可能是一个错误?)。 Thus, the sample above does actually work as expected.因此,上面的示例确实按预期工作。

I think the premise of your question isn't correct, and you may have come to the conclusion in your question due to the fact that the window title of the newly opened window is only updated to reflect administrator status a short while after the window opens .我认为您的问题的前提是不正确的,并且您可能已经得出了您的问题的结论,因为新打开的 window 的window 标题仅在 Z05B8C74CBD976FBF2DE4C1A3542不久更新以反映管理员状态.

However, the process associated with the window is elevated from the very beginning , as the following command demonstrates:但是,与 window 相关的进程从一开始就提升,如以下命令所示:

# Opens an elevated cmd session in a new window and executes
# `net session`, which only succeeds with elevation.
Start-Process cmd -ArgumentList '/k net session' -Verb RunAs

The net session command succeeds, implying that the process is running with elevation (irrespective of how long it takes the window title to reflect that fact). net session命令成功,这意味着该进程正在以高程运行(无论 window 标题反映该事实需要多长时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM