CMD命令行中以管理员权限启动应用程序实现方法

所属分类: 脚本专栏 / DOS/BAT 阅读数: 1064
收藏 0 赞 0 分享

很多时候我们需要管理员权限来运行bat那么就需要结合vbscript来实现了

方法一:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

常用

@echo off
mode con lines=30 cols=60
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
rem 下面可以写你的bat代码了

方法二:

@echo off 
%1 %2 
ver|find "5.">nul&&goto :st 
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st","","runas",1)(window.close)&goto :eof 
:st 
copy "%~0" "%windir%\system32\" 

原理类似

ShellExecute method

Run a script or application in the Windows Shell.

Syntax
.ShellExecute "application", "parameters", "dir", "verb", window

.ShellExecute 'some program.exe', '"some parameters with spaces"', , "runas", 1
Key
application The file to execute (required)
parameters Arguments for the executable
dir Working directory
verb The operation to execute (runas/open/edit/print)
window View mode application window (normal=1, hide=0, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default)
Note the different (double " and single ' ) quotes that can be used to delimit paths with spaces.

The runas verb is undocumented but can be used to elevate permissions. When a script is run with elevated permissions several aspects of the user environment may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.

runas will fail if you are running in WOW64 (a 32 bit process on 64 bit windows) for example %systemroot%\syswow64\cmd.exe ...

The ShellExecute method is a member of the IShellDispatch2 object.

Examples

Run a batch script with elevated permissions, flag=runas:

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "E:\demo\batchScript.cmd", "", "", "runas", 1

Run a VBScript with elevated permissions, flag=runas:

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cscript", "E:\demo\vbscript.vbs", "", "runas", 1

“If you don't execute your ideas, they die” ~ Roger Von Oech

Related:

Run with elevated permissions - Script to run as Admin
.Exec - Execute command, returning an object
.Run - Run a command
joeware.net - CPAU (Create Process As User) like RunAs but with an options to encrypt the password.
Equivalent CMD command: ShellRunAs - Run a command under a different user account

 批处理文件中的%~dp0表示含义

~是扩展的意思,相当于把一个相对路径转换绝对路径
%0代指批处理文件自身
%1表示批处理文件命令行接收到的第一个参数,%2表示第二个,以此类推
%~d0 是指批处理所在的盘符,其中d代表drive
%~p0 是指批处理所在的目录,其中p代表path
%~dp0 是批处理所在的盘符加路径


cd %~dp0 就是进入批处理所在目录了


详细解释还可参考命令 call /?

自从Vista带来了UAC之后,应用程序就变成了两种,有管理员权限的,和没有管理员权限的。一些老的应用程序会莫名其妙地出错,这时候就要考虑右击应用程序,然后“以管理员身份运行”。这还不是什么大问题,exe文件的右键菜单里都会有这个,但是对于一些脚本文件(cmd, js一类)来说,就没那么方便了。通常需要重新开一个带管理员权限的命令行窗口,然后打很多cd回到刚的文件夹,然后再运行脚本,相当麻烦。

搜了一下,找到一个解决办法。把下面的代码保存为Elevate.js:

var command = WScript.Arguments.Item(0);
var argument = "";
for (var i = 0; i < WScript.Arguments.Count(); ++i){
 argument += WScript.Arguments.Item(i) + " ";
}
 
try{
 var shellapp = new ActiveXObject("Shell.Application");
 shellapp.ShellExecute(command, argument, null, "runas", 1);
}
catch(e){
 WScript.Echo("Something wrong: " + e.description);
}

以后要以管理员身份运行程序的时候,只要输入“Elevate <exefile> <arguments>”就可以了,比如“Elevate cmd /k”。

当然,这个逃不过UAC的检查,还是会有一个对话框弹出来要点“确定”的。

更多精彩内容其他人还在看

for 语句从入门到精通

在批处理中,for是最为强大的命令语句,它的出现,使得解析文本内容、遍历文件路径、数值递增/递减等操作成为可能
收藏 0 赞 0 分享

dos 目录跳转 cd

当我们需要处理不同路径下的文件的时候,很可能需要切换目录,这个时候,可以考虑使用目录跳转命令cd。
收藏 0 赞 0 分享

tree 以树形格式罗列文件

tree ,在英语中的基本含义是“树”,在cmd中,tree命令的功能是以树形格式罗列文件。
收藏 0 赞 0 分享

dos 内容重定向

当我们在cmd窗口中查询某条命令的帮助信息的时候,帮助信息是显示在命令行窗口中的,命令行窗口关闭后,这些帮助信息就看不到了,如果下次还想看,又得在命令行窗口中输入查询命令,比较繁琐。
收藏 0 赞 0 分享

执行批处理bat程序中的条件处理

一直用bat实现部分功能,对于我们仍需要控制是否满意我们的要求,下面是if帮助文档,方便查询
收藏 0 赞 0 分享

不错的批处理脚本 第一部分

非常不错的批处理脚本代码,功能比较多,用到了,很多的批处理机器
收藏 0 赞 0 分享

不错的批处理脚本实例代码 第二部分

不错的批处脚本实例代码,用到了批处理中的很多技巧与知识点,不懂得可以逐一查找相关资料
收藏 0 赞 0 分享

开机更新桌面主题的批处理代码

更新桌面主题的批处理代码
收藏 0 赞 0 分享

非常好的for 教程, 当时我就是看这个学习for 的第1/2页

批处理for命令详解 FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号)
收藏 0 赞 0 分享

批处理的"循环"效果脚本

曾经在回答一个问题时 无意中想到的方法 今天又看到类似的问题 个人认为是非常实用的 于是 想把这种方法推荐给大家
收藏 0 赞 0 分享
查看更多