win10 批處理設置靜態ip地址


如下代碼另存為xxx.bat。 點擊運行即可

@echo off
rem 強制獲取管理員權限
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
rem //設置變量
set NAME="vEthernet (Default Switch)"
rem //以下屬性值可以根據需要更改
set ADDR=x.22.96.1
set MASK=255.255.240.0
set GATEWAY=
set DNS1=
set DNS2=
rem //以上屬性依次為IP地址、子網掩碼、網關、首選DNS、備用DNS


echo Select your operation:
echo 1 set static IP
echo 2 set dynamic IP
echo 3 quit
echo please input you selection with enter:

rem 直接設置靜態ip地址:

goto 1
rem set /p operate=
rem if %operate%==1 goto 1
if %operate%==2 goto 2
if %operate%==3 goto 3


:1
echo setting IP]...
rem //可以根據你的需要更改
echo IP = %ADDR%
echo mask = %MASK%
echo gateway = %GATEWAY%
netsh interface ipv4 set address %NAME% static %ADDR% %MASK% %GATEWAY%
echo pri. DNS = %DNS1%
if "%DNS1%"=="" (echo DNS1 empty) else netsh interface ipv4 set dns %NAME% static %DNS1%
echo backup DNS = %DNS2%
if "%DNS2%"=="" (echo DNS2 empty) else (netsh interface ipv4 add dns %NAME% %DNS2%)
echo -------setted static IP: %ADDR%--------
rem Get-NetIPConfiguration
ipconfig /all
pause
goto 3


:2
echo 正在設置動態IP,請稍等...
echo 正在從DHCP自動獲取IP地址...
netsh interface ip set address %NAME% dhcp
echo 正在從DHCP自動獲取DNS地址...
netsh interface ip set dns %NAME% dhcp
echo **********已設置為動態IP地址***********
pause
goto 3


:3
exit

 

=================================

關於以mshta vbscript:CreateObject更多說明:

CMD命令行中以管理員權限啟動應用程序實現方法
很多時候我們需要管理員權限來運行bat那么就需要結合vbscript來實現了
方法一:
%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代碼了

-----
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

批處理文件中的%~dp0表示含義
~是擴展的意思,相當於把一個相對路徑轉換絕對路徑
%0代指批處理文件自身
%1表示批處理文件命令行接收到的第一個參數,%2表示第二個,以此類推
%~d0 是指批處理所在的盤符,其中d代表drive
%~p0 是指批處理所在的目錄,其中p代表path
%~dp0 是批處理所在的盤符加路徑
cd %~dp0 就是進入批處理所在目錄了
參考:https://www.jb51.net/article/67623.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM