脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - VBS - CMD命令行中以管理员权限启动应用程序实现方法

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

2020-07-04 09:48脚本之家 VBS

这篇文章主要介绍了CMD命令行中以管理员权限启动应用程序实现方法,本文使用一个JS脚本来实现,需要的朋友可以参考下

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

方法一:

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

常用

?
1
2
3
4
5
@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代码了

方法二:

?
1
2
3
4
5
6
@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:

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

Run a VBScript with elevated permissions, flag=runas:

?
1
2
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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的检查,还是会有一个对话框弹出来要点“确定”的。

延伸 · 阅读

精彩推荐
  • VBSVBS编程教程 (第1篇)

    VBS编程教程 (第1篇)

    VBScript的全称是:Microsoft Visual Basic Script Editon.(微软公司可视化BASIC脚本版). 正如其字面所透露的信息, VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言....

    VBS教程网4812020-07-15
  • VBS灵活实用VBS入门教程应用篇

    灵活实用VBS入门教程应用篇

    上一篇文章我们了解了VBS编程的一些基础知识,要更深入地学习还要学习选择结构和循环结构。 ...

    VBS教程网5742020-07-01
  • VBSVBS教程:正则表达式简介 -正则表达式语法

    VBS教程:正则表达式简介 -正则表达式语法

    正则表达式语法 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的...

    VBS教程网3302019-11-25
  • VBS雷客图ASP站长安全助手vbs测试版代码

    雷客图ASP站长安全助手vbs测试版代码

    雷客图ASP站长安全助手是一个基于ASP的帮助站长维护网站安全的程序。这个版本(vbs测试版)主要用于服务器本地运行以查找ASP木马。此版本为测试版,希...

    脚本之家2312020-07-03
  • VBSWINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

    WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

    本文主要分享WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码,有需要的童鞋可以参考下...

    脚本之家3232020-07-06
  • VBSVBS数组深入浅出

    VBS数组深入浅出

    VBS数组在应用中没有像其他语句那么广泛,VBS数组存在不少功能上的局限性(如二维数组的定义、赋值),在使用上也没有java等语言那么便捷...

    VBS教程网2962020-08-18
  • VBSVBS文本文件操作实现代码

    VBS文本文件操作实现代码

    这篇文章主要介绍了VBS文本文件操作实现代码,需要的朋友可以参考下...

    VBS代码网8912020-08-12
  • VBSAdsutil.vbs 在脚本攻击中的妙用[我非我原创]

    Adsutil.vbs 在脚本攻击中的妙用[我非我原创]

    adsutil.vbs是什么?相信用过IIS的网管员不会不知道。这是IIS自带的提供于命令行下管理IIS的一个脚本。位于%SystemDrive%\Inetpub\AdminScripts目录下。...

    VBS教程网2312020-07-07