run CMD as administrator in PowerShell
Asked Answered
B

3

5

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
Briefing answered 11/11, 2018 at 12:55 Comment(0)
M
7

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

Note:

  • To make the invocation - which invariably executes in a new window - synchronous, add
    -Wait.

  • Unless the process you're calling from is itself already elevated, you'll get an interactive UAC (User Account Control) prompt.

    • While it is possible to configure your system to not require this prompt, doing so is ill-advised for security reasons - see this answer.
Macadam answered 11/11, 2018 at 13:18 Comment(2)
is there a way to do this without a window prompting asking yes or no. I mean I want to open the x64 native tools for VS cmd, but if I use Start-Process -Verb RunAs "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" a windows pop up asking yes or no, as I need to use it in a jenkins pipeline it will need to be automatized, is there any parameter or workaround to do that?Locoism
@JesusFernandez, this confirmation dialog is an important security feature (UAC). While it is possible to deactivate it, doing so is a serious security risk. Do you really need to run your console as an administrator? If so, perhaps Jenkins itself can be configured to launch a process elevated, then you wouldn't need Start-Process -Verb RunAs.Macadam
C
0

@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"
Camber answered 3/12, 2021 at 22:42 Comment(0)
P
-1

Type this command:

runas /noprofile /user:Administrator cmd 

Then enter the Administrator password.

Pratique answered 11/11, 2018 at 13:4 Comment(2)
This only works with the built-in Adminstrator account, which is typically disabled for security reasons. Otherwise, runas.exe doesn't support on-demand elevation from a non-elevated process for other accounts. For instance, if your own account does have administrative privileges in principle, but your current shell is not elevated, runas offers no way launch another process as you with elevation.Macadam
but this method asks me for a password, therefore I cannot use it in automated systems such as jenkins. Or is there any way to pass the password as a parameter or something like it?Locoism

© 2022 - 2024 — McMap. All rights reserved.