溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)CMD命令行中以管理員權(quán)限啟動應(yīng)用程序

發(fā)布時間:2021-09-28 11:15:07 來源:億速云 閱讀:586 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何實(shí)現(xiàn)CMD命令行中以管理員權(quán)限啟動應(yīng)用程序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何實(shí)現(xiàn)CMD命令行中以管理員權(quán)限啟動應(yīng)用程序”吧!

很多時候我們需要管理員權(quán)限來運(yùn)行bat那么就需要結(jié)合vbscript來實(shí)現(xiàn)了

方法一:

%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 IShellDispatch3 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表示含義

~是擴(kuò)展的意思,相當(dāng)于把一個相對路徑轉(zhuǎn)換絕對路徑
%0代指批處理文件自身
%1表示批處理文件命令行接收到的第一個參數(shù),%2表示第二個,以此類推
%~d0 是指批處理所在的盤符,其中d代表drive
%~p0 是指批處理所在的目錄,其中p代表path
%~dp0 是批處理所在的盤符加路徑

cd %~dp0 就是進(jìn)入批處理所在目錄了

詳細(xì)解釋還可參考命令 call /?

自從Vista帶來了UAC之后,應(yīng)用程序就變成了兩種,有管理員權(quán)限的,和沒有管理員權(quán)限的。一些老的應(yīng)用程序會莫名其妙地出錯,這時候就要考慮右擊應(yīng)用程序,然后“以管理員身份運(yùn)行”。這還不是什么大問題,exe文件的右鍵菜單里都會有這個,但是對于一些腳本文件(cmd, js一類)來說,就沒那么方便了。通常需要重新開一個帶管理員權(quán)限的命令行窗口,然后打很多cd回到剛的文件夾,然后再運(yùn)行腳本,相當(dāng)麻煩。

搜了一下,找到一個解決辦法。把下面的代碼保存為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);
}

以后要以管理員身份運(yùn)行程序的時候,只要輸入“Elevate <exefile> <arguments>”就可以了,比如“Elevate cmd /k”。

當(dāng)然,這個逃不過UAC的檢查,還是會有一個對話框彈出來要點(diǎn)“確定”的。

到此,相信大家對“如何實(shí)現(xiàn)CMD命令行中以管理員權(quán)限啟動應(yīng)用程序”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

cmd
AI