国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

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

2019-11-06 08:48:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法,本文使用一個(gè)JS腳本來(lái)實(shí)現(xiàn),需要的朋友可以參考下

很多時(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 offmode con lines=30 cols=60%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exitcd /d "%~dp0"rem 下面可以寫(xiě)你的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 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 commandjoeware.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 是指批處理所在的盤(pán)符,其中d代表drive%~p0 是指批處理所在的目錄,其中p代表path%~dp0 是批處理所在的盤(pán)符加路徑

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)“確定”的。

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 茶陵县| 东安县| 广州市| 堆龙德庆县| 综艺| 宁国市| 宜川县| 高尔夫| 托克逊县| 奈曼旗| 翼城县| 旬阳县| 牙克石市| 奎屯市| 抚顺县| 汝州市| 山阳县| 玛曲县| 交城县| 阳原县| 文水县| 青海省| 南充市| 犍为县| 汶上县| 安岳县| 巴林右旗| 行唐县| 德州市| 房山区| 株洲市| 麻阳| 石台县| 什邡市| 出国| 东丰县| 上虞市| 大埔县| 晋宁县| 蓬安县| 平果县|