如何从命令提示符“以管理员身份运行”运行应用程序?

时间:2021-01-02 20:21:22

I have a batch file called test.bat. I am calling the below instructions in the test.bat file:

我有一个名为test.bat的批处理文件。我在test.bat文件中调用以下说明:

start /min powershell.exe %sysdrive%\testScripts\testscript1.ps1

When I run this through the command prompt, my testscript is running successfully. I want to run it as administrator (as if I have created a desktop shortcut and run as administrator. It shouldn't prompt for any username or password).

当我通过命令提示符运行时,我的testscript运行成功。我想以管理员身份运行它(就像我创建了一个桌面快捷方式并以管理员身份运行。它不应该提示任何用户名或密码)。

I have tried adding /elevate and /NOUAC parameters in the above test.bat, but no luck. How do I fix this issue?

我已经尝试在上面的test.bat中添加/ elevate和/ NOUAC参数,但没有运气。我该如何解决这个问题?

I know how to do it manually, but I want this to be executed from the command prompt.

我知道如何手动执行,但我希望从命令提示符执行此操作。

(By Marnix Klooster): ...without using any additional tools, like those suggested in an answer to Super User question How to run program from command line with elevated rights.)

(作者Marnix Klooster):...不使用任何其他工具,例如在回答超级用户问题时建议的那些工具如何从具有提升权限的命令行运行程序。)

3 个解决方案

#1


81  

Try this:

尝试这个:

runas.exe /savecred /user:administrator "%sysdrive%\testScripts\testscript1.ps1" 

It saves the password the first time and never asks again. Maybe when you change the administrator password you will be prompted again.

它第一次保存密码,永远不会再问。也许当您更改管理员密码时,系统会再次提示您。

#2


21  

See this TechNet article: Runas command documentation

请参阅此TechNet文章:Runas命令文档

From a command prompt:

从命令提示符:

C:\> runas /user:<localmachinename>\administrator cmd

Or, if you're connected to a domain:

或者,如果您已连接到域:

C:\> runas /user:<DomainName>\<AdministratorAccountName> cmd

#3


6  

It looks like psexec -h is the way to do this:

看起来像psexec -h是这样做的方法:

 -h         If the target system is Windows Vista or higher, has the process
            run with the account's elevated token, if available.

Which... doesn't seem to be listed in the online documentation in Sysinternals - PsExec.

哪个......似乎没有在Sysinternals的在线文档中列出 - PsExec。

But it works on my machine.

但它适用于我的机器。

#1


81  

Try this:

尝试这个:

runas.exe /savecred /user:administrator "%sysdrive%\testScripts\testscript1.ps1" 

It saves the password the first time and never asks again. Maybe when you change the administrator password you will be prompted again.

它第一次保存密码,永远不会再问。也许当您更改管理员密码时,系统会再次提示您。

#2


21  

See this TechNet article: Runas command documentation

请参阅此TechNet文章:Runas命令文档

From a command prompt:

从命令提示符:

C:\> runas /user:<localmachinename>\administrator cmd

Or, if you're connected to a domain:

或者,如果您已连接到域:

C:\> runas /user:<DomainName>\<AdministratorAccountName> cmd

#3


6  

It looks like psexec -h is the way to do this:

看起来像psexec -h是这样做的方法:

 -h         If the target system is Windows Vista or higher, has the process
            run with the account's elevated token, if available.

Which... doesn't seem to be listed in the online documentation in Sysinternals - PsExec.

哪个......似乎没有在Sysinternals的在线文档中列出 - PsExec。

But it works on my machine.

但它适用于我的机器。