如何以管理员身份从 Windows 命令行运行命令? [英] How do you run a command as an administrator from the Windows command line?

查看:34
本文介绍了如何以管理员身份从 Windows 命令行运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小脚本,可以在 Windows 上为 Bazaar<执行构建和安装过程/a> 我正在管理的存储库.我正在尝试从 Windows shell (cmd.exe) 的以提升的管理权限运行脚本——就像我右键单击它并选择以管理员身份运行一样,但不使用任何需要使用图形界面的方法.

I have a small script that performs the build and install process on Windows for a Bazaar repository I'm managing. I'm trying to run the script with elevated, administrative privileges from within the Windows shell (cmd.exe)--just as if I'd right-clicked it and chosen Run as Administrator, but without using any method that requires use of the graphical interface.

推荐答案

批处理/WSH 混合能够调用 ShellExecute 来显示 UAC 提升对话框...

A batch/WSH hybrid is able to call ShellExecute to display the UAC elevation dialog...

@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
>nul 2>&1 "%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configsystem"||(
    cscript //E:JScript //nologo "%~f0"
    @goto :EOF
)
echo.Performing admin tasks...
REM call foo.exe
@goto :EOF
@end @ELSE
ShA=new ActiveXObject("Shell.Application")
ShA.ShellExecute("cmd.exe","/c ""+WScript.ScriptFullName+""","","runas",5);
@end

这篇关于如何以管理员身份从 Windows 命令行运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆