激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

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的檢查,還是會有一個對話框彈出來要點“確定”的。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩视频高清 | 日日艹夜夜艹 | 精品国产专区 | 国产盼盼私拍福利视频99 | 337p粉嫩大胆噜噜噜亚瑟影院 | 亚洲成人福利网站 | 在线播放91 | 久久夜夜视频 | 国产精品99久久久久久宅女 | 毛片a区| 97黄色网| 最新日韩精品在线观看 | 午夜视频在线观看91 | 毛片免费在线视频 | 成人免费在线网 | 国产精品久久久久久久不卡 | 看免费一级毛片 | 哪里可以看免费的av | 黄在线观看在线播放720p | 一级做人爱c黑人影片 | 一区二区三区精品国产 | 中文字幕h | 黄色片在线播放 | 黄网站免费观看视频 | 亚洲成人精品国产 | 国产青草网 | 国产盼盼私拍福利视频99 | 美女黄污视频 | 福利在线国产 | 欧美国产免费 | 51色视频 | 一级α片免费看刺激高潮视频 | 综合精品在线 | 97超级碰碰人国产在线观看 | 国产一区二区免费在线观看 | 久久新网址 | 国产一区二区视频在线播放 | 亚洲一区在线不卡 | 国产一区二区精彩视频 | 日韩视频观看 | 免费a级网站 |