最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2024年07月16日 12:48:11   投稿:junjie  
這篇文章主要介紹了CMD命令行中以管理員權(quán)限啟動(dòng)應(yīng)用程序?qū)崿F(xiàn)方法,本文使用一個(gè)JS腳本來實(shí)現(xiàn),需要的朋友可以參考下

很多時(shí)候我們需要管理員權(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\" 

代碼三

::UAC提權(quán)
@echo off&(cd/d "%~dp0")&(cacls "%SystemDrive%\System Volume Information" >nul 2>&1)||(start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0"," %*","","runas",1^)^(window.close^)&exit /b)

::后面寫你自己的腳本

@echo ON

代碼四

@ECHO OFF&(PUSHD "%~DP0")&(REG QUERY "HKU\S-1-5-19">NUL 2>&1)||(
powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&EXIT)

判斷是否以管理員身份運(yùn)行

這個(gè)操作方式比較多,核心思想就是試圖訪問需要管理員身份才可以訪問的資源,可以訪問則有權(quán)限,不可以訪問則沒有權(quán)限
以下三種方式都可以進(jìn)行判斷,可以靈活使用

"%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REG QUERY "HKU\S-1-5-19"
net.exe session

使用管理員權(quán)限運(yùn)行

提供兩種方式

VBS方式

mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)

powershell方式

%~sdpnx0: 指向當(dāng)前批處理文件

powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"

powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&EXIT

這是一個(gè)PowerShell命令,用于以管理員權(quán)限啟動(dòng)當(dāng)前執(zhí)行的腳本。

這段代碼的主要部分是 Start-Process '%~sdpnx0' -Verb RunAs,其中 %~sdpnx0 是一個(gè)批處理腳本中的變量替換,表示當(dāng)前執(zhí)行的批處理腳本文件的全路徑名。-Verb RunAs 參數(shù)指示 Start-Process 使用提升的權(quán)限運(yùn)行進(jìn)程。

但是,這個(gè)命令在 PowerShell 中并不會(huì)直接以管理員權(quán)限運(yùn)行當(dāng)前腳本,因?yàn)?nbsp;%~sdpnx0 這個(gè)變量替換在 PowerShell 中不會(huì)被正確解析。在 PowerShell 中,你可以使用 $PSCommandPath 變量來獲取當(dāng)前腳本的完整路徑,然后使用 Start-Process 命令以管理員權(quán)限運(yùn)行它。

下面是一個(gè)修改后的 PowerShell 命令,它可以以管理員權(quán)限運(yùn)行當(dāng)前腳本:

Start-Process "$PSCommandPath" -Verb RunAs

如果你想要這個(gè)命令在執(zhí)行后自動(dòng)退出,你可以在命令的末尾添加 & exit,如下所示:

Start-Process "$PSCommandPath" -Verb RunAs & exit

請(qǐng)注意,這些命令應(yīng)該在 PowerShell 腳本中使用,而不是在批處理文件中。批處理文件中應(yīng)使用不同的語(yǔ)法。

組合使用

提供兩種組合方式,代碼放于批處理前面使用即可,其他自由組合

:: 方式一
REG QUERY "HKU\S-1-5-19">NUL 2>&1||(powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&exit)
:: 方式二
REG QUERY "HKU\S-1-5-19">NUL 2>&1||mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

原理類似

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帶來了UAC之后,應(yīng)用程序就變成了兩種,有管理員權(quán)限的,和沒有管理員權(quán)限的。一些老的應(yīng)用程序會(huì)莫名其妙地出錯(cuò),這時(shí)候就要考慮右擊應(yīng)用程序,然后“以管理員身份運(yùn)行”。這還不是什么大問題,exe文件的右鍵菜單里都會(huì)有這個(gè),但是對(duì)于一些腳本文件(cmd, js一類)來說,就沒那么方便了。通常需要重新開一個(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è)逃不過UAC的檢查,還是會(huì)有一個(gè)對(duì)話框彈出來要點(diǎn)“確定”的。

相關(guān)文章

最新評(píng)論

鄂伦春自治旗| 邓州市| 扎鲁特旗| 呼伦贝尔市| 庆阳市| 沁源县| 泸溪县| 北海市| 黎平县| 凯里市| 婺源县| 马鞍山市| 唐河县| 大连市| 当雄县| 石屏县| 简阳市| 如皋市| 长宁县| 灵璧县| 威海市| 西吉县| 达孜县| 洛宁县| 兰西县| 密云县| 桃源县| 盘锦市| 鄢陵县| 油尖旺区| 华池县| 和林格尔县| 横山县| 都昌县| 吉水县| 永州市| 彰武县| 治县。| 奎屯市| 怀宁县| 寿光市|