相关文章推荐
怕老婆的米饭  ·  NETSDK1004: ...·  1 年前    · 
气宇轩昂的椅子  ·  Python ...·  1 年前    · 
%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 
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) + " ";
 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的检查,还是会有一个对话框弹出来要点“确定”的。

原文地址: http://www.jb51.net/article/67623.htm

这篇文章主要介绍了CMD命令行中以管理员权限启动应用程序实现方法,本文使用一个JS脚本来实现,需要的朋友可以参考下很多时候我们需要管理员权限来运行bat那么就需要结合vbscript来实现了方法一:%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::" 需要.NET Framework 4.5和Windows 7-10! 带参数运行 使用一个参数运行实用程序的可执行文件,该参数将包含以管理员身份运行的文件的路径/名称 cmd的示例: RunAdminCMD C:\Windows\explorer.exe 0 cmd示例(带有“空格”): 如果到/文件名的路径带有“空格”,则必须在引号(“”)中加上引号 RunAdminCMD "C:\scripts wifi\Enable WiFi.cmd" 0 cmd的示例(其他文件带有1个参数): RunAdminCMD PingCMD.bat google.co
cd /d %~dp0 %1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit %1 mshta vbscript:CreateObject(“Shell.Application”).ShellExe...
如何使用使用CMD命令管理员权限打开某个路径下的EXE程序? 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。 我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功...
::在你的脚本前加上以下两句命令 %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit cd /d "%~dp0" ::下面是你的程序 tscancode.exe --enable=warning E:\Gbmp\gdc-sdk\src\Common 2>tscancode %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c ""%~s0"" ::","","runas",1)(window.close)&&exit 然后直接双击bat文件运行即可 使用SHELLEXECUTEINFO,然后生成root.exe可执行文件,然后将root.exe放到c:\\windows\\system32(或者环境变量PATH下的其他 代码,root.cpp,使用的vc 6++编译 #include <windows.h> #include <stdio.h> extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow (); VOID..
最近在项目中偶然用到了使用VBS调用“shell.application”中的ShellExecute方法。在百度搜索一圈后发现很难找到关于这个方法的详细的中文教程。最后是在微软的官方文档中才找到了这个指令的详细英文教程。现将该方法的详细描述用中文分享给各位码友。 首先,VBS中调用该方法可以用Set Shell=CreateObject("Shell.Application")来创建对象。该方法的命令格式如下: Shell.ShellExecute "执行命令或文件的名称", "执行命令的附加参数",
方法三:纯键盘模式 点开开始,在开始的输入框中输入cmd,这个时候出现cmd的快捷方式,可以采用方法一的方法启用管理员调用,但这里我们不用鼠标,直接按住CTRL+SHIFT+ENTER三个键就可以直接调用管理员权限cmd了; 方法四:命令模式