bat實現往hosts文件追加內容

作個筆記。bash

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::ADMIN POWER(start)::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::ADMIN POWER(end):::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set a=192.168.13.11 chenglee-001
set file=C:\Windows\System32\drivers\etc\hosts

::(echo.) >> %file%
(echo %a%) >> %file%

echo 寫入成功...5秒後退出窗口
choice /t 5 /d y /n >nul
::pause
exit

第一塊獲取管理員admin權限, 第二塊實現寫入, 去掉不相關空格。blog

相關文章
相關標籤/搜索