简体   繁体   中英

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.

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. How can I get this to run tool.exe as Administrator?

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?). 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 .

However, the process associated with the window is elevated from the very beginning , as the following command demonstrates:

# 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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