How do I run a program from command prompt as a di

2019-01-21 18:35发布

I am using "runas" to open command prompt as a different user but that command prompt is not running as an admin. How can I make it run as an admin?

UPDATE: I am using Windows Server 2012

UPDATE: I opened cmd for another account by running

 runas /user:domain\username cmd.exe

Then I tried to run some commands in this new prompt but this is not running as an elevated user (even though it has Administrator privileges).

9条回答
Rolldiameter
2楼-- · 2019-01-21 19:04

Runas doesn't magically run commands as an administrator, it runs them as whatever account you provide credentials for. If it's not an administrator account, runas doesn't care.

查看更多
来,给爷笑一个
3楼-- · 2019-01-21 19:07

In my case I was already logged in as a local administrator and I needed to run CMD as a domain admin so what worked for me was running the below from a powershell window:

runas /noprofile /user:DOMAIN\USER "cmd"

查看更多
Fickle 薄情
4楼-- · 2019-01-21 19:09

See here: https://superuser.com/questions/42537/is-there-any-sudo-command-for-windows

According to that the command looks like this for admin:

 runas /noprofile /user:Administrator cmd
查看更多
唯我独甜
5楼-- · 2019-01-21 19:16

Start -> shift + command Prompt right click will helps to use as another user or as Admin

查看更多
Melony?
6楼-- · 2019-01-21 19:21

All of these answers unfortunately miss the point.

There are 2 security context nuances here, and we need them to overlap. - "Run as administrator" - changing your execution level on your local machine - "Run as different user" - selects what user credentials you run the process under.

When UAC is enabled on a workstation, there are processes which refuse to run unless elevated - simply being a member of the local "Administrators" group isn't enough. If your requirement also dictates that you use alternate credentials to those you are signed in with, we need a method to invoke the process both as the alternate credentials AND elevated.

What I found can be used, though a bit of a hassle, is:

  • run a CMD prompt as administrator
  • use the Sysinternals psexec utility as follows:

    psexec \\localworkstation -h -i -u domain\otheruser exetorun.exe

The first elevation is needed to be able to push the psexec service. The -h runs the new "remote" (local) process elevated, and -i lets it interact with the desktop.

Perhaps there are easier ways than this?

查看更多
在下西门庆
7楼-- · 2019-01-21 19:23

I've found a way to do this with a single line:

runas /user:DOMAIN\USER2 /savecred "powershell -c start-process -FilePath \"'C:\\PATH\\TO\\YOUR\\EXECUTABLE.EXE'\" -verb runAs"

There are a few tricks going on here.

1: We are telling CMD just to run Powershell as DOMAIN\USER2

2: We are passing the "Start-Process" command to Powershell, using the verb "runAs" to elevate DOMAIN\USER2 to Administrator/Elevated privilege mode.

As a general note, the escape characters in the "FilePath" argument must be present (in other words, the "\ & \\ character combinations), and the single quotation (') must surround the EXE path - this way, CMD interprets the FilePath as a single string, then Powershell uses the single quotation to interpret the FilePath as a single argument.

Using the "RunAs" verb to elevate within Powershell: http://ss64.com/ps/syntax-elevate.html

查看更多
登录 后发表回答