濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法

CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法

熱門標(biāo)簽:智能打電話機(jī)器人收費(fèi) 雷霆電銷機(jī)器人怎么樣 如何弄地圖標(biāo)注 電話機(jī)器人產(chǎn)品怎么樣 呼倫貝爾外呼系統(tǒng) 清遠(yuǎn)百度地圖標(biāo)注店鋪位置 電話機(jī)器人全國(guó)招商 如何申請(qǐng)400電話業(yè)務(wù)mm 玉林市機(jī)器人外呼系統(tǒng)哪家好

很多時(shí)候我們需要管理員權(quán)限來(lái)運(yùn)行bat那么就需要結(jié)合vbscript來(lái)實(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 下面可以寫(xiě)你的bat代碼了

方法二:

@echo off 
%1 %2 
ver|find "5.">nulgoto :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表示含義

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


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


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

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

搜了一下,找到一個(gè)解決辦法。把下面的代碼保存為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)行程序的時(shí)候,只要輸入“Elevate exefile> arguments>”就可以了,比如“Elevate cmd /k”。

當(dāng)然,這個(gè)逃不過(guò)UAC的檢查,還是會(huì)有一個(gè)對(duì)話框彈出來(lái)要點(diǎn)“確定”的。

您可能感興趣的文章:
  • JSP 2.1和JSF 1.2規(guī)范發(fā)布預(yù)覽版本
  • js 編寫(xiě)規(guī)范
  • JSDoc 介紹使用規(guī)范JsDoc的使用介紹
  • Javascript模塊化編程(一)AMD規(guī)范(規(guī)范使用模塊)
  • Java Web項(xiàng)目前端規(guī)范(采用命名空間使js深度解耦合)
  • AMD異步模塊定義介紹和Require.js中使用jQuery及jQuery插件的方法
  • Node.js編碼規(guī)范
  • PowerShell中使用Get-Alias命令獲取cmdlet別名例子
  • C#調(diào)用CMD命令實(shí)例
  • Windows Powershell 命令集 cmdlets
  • PHP中執(zhí)行cmd命令的方法
  • PHP啟動(dòng)windows應(yīng)用程序、執(zhí)行bat批處理、執(zhí)行cmd命令的方法(exec、system函數(shù)詳解)
  • java執(zhí)行windows下cmd命令的方法
  • bat/cmd批處理連接SqlServer數(shù)據(jù)庫(kù)查詢腳本
  • Python使用CMD模塊更優(yōu)雅的運(yùn)行腳本
  • C#隱式運(yùn)行CMD命令(隱藏命令窗口)
  • JavaScript模塊規(guī)范之AMD規(guī)范和CMD規(guī)范

標(biāo)簽:臺(tái)州 蕪湖 江西 株洲 樂(lè)山 公主嶺 白銀 三門峽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法》,本文關(guān)鍵詞  CMD,命令,行中,以,管理員,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    万年县| 香河县| 双牌县| 阿瓦提县| 德安县| 江阴市| 邯郸市| 孝义市| 金秀| 江陵县| 杂多县| 罗田县| 新巴尔虎左旗| 集安市| 温州市| 保山市| 庄河市| 浮山县| 青田县| 潞西市| 镇原县| 社会| 平泉县| 苍南县| 盐城市| 五台县| 汉阴县| 昭觉县| 临澧县| 六枝特区| 德令哈市| 平塘县| 望江县| 宁晋县| 梓潼县| 兰考县| 遵化市| 甘德县| 天峻县| 故城县| 长海县|