运行 在 PowerShell 中以管理员身份运行 CMD

run CMD as administrator in PowerShell

我正在尝试使用 powershell 以管理员身份执行命令提示符。 (就像当您右键单击 cmd 图标并选择 运行 作为管理员时)。 为此,我应该在以下内容中添加什么?

& cmd.exe /c $VAR

键入此命令:

runas /noprofile /user:Administrator cmd 

然后输入管理员密码。

有些晦涩,您必须使用带有参数 -Verb RunAsStart-Process 才能在 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

@Roei Givati - 事实上,我刚刚自己为 Jenkins 解决了这个问题! 从任务计划程序中设置下面的命令(您可以从 jenkins 仪表板上的节点页面获取它),直接使用整个路径,并为任务计划程序中的选项提供 /K。当然,我也设置了路径。然后您可以使用非交互式 svc 帐户将进程作为 cmd 文件启动:

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