简体   繁体   中英

run CMD as administrator in PowerShell

I'm trying to execute a command prompt as administrator by using powershell. (like when you press right click on cmd icon and choose run as administrator). what should I add to the following in order to do so?

& cmd.exe /c $VAR

Somewhat obscurely, you must use Start-Process with argument -Verb RunAs in order to launch an elevated process (a process with administrative privileges) in PowerShell:

# The command to pass to cmd.exe /c
$var = 'echo hello world & pause'

# Start the process asynchronously, in a new window,
# as the current user with elevation (administrative rights).
# Note the need to pass the arguments to cmd.exe as an *array*.
Start-Process -Verb RunAs cmd.exe -Args '/c', $var

Type this command:

runas /noprofile /user:Administrator cmd 

Then enter the Administrator password.

@Roei Givati - I just solved this one myself for Jenkins, in fact! From a task scheduler set this command below (you get it from the nodes page on your jenkins dashboard), directly with the whole path, and give it the /K for the option in task scheduler. I also set the path as well, to be sure. Then you can use a non-interactive svc account to launch the process as a cmd file:

java -jar agent.jar -jnlpUrl https://jenkins-path/nts4/computer//slave-agent.jnlp -secret f7351fa6d6774765432111b704cfd777931144bb32c42 -workDir "driveletter:path\nts4"

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