win10批处理设置静态ip地址
如下代码另存为xxx.bat。点击运⾏即可
@echo off
rem 强制获取管理员权限
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("","/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("","/c %~s0 ::","","runas",1)(window.close)&&exit
常⽤如下:
@echo off
mode con lines=30 cols=60
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("","/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 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%\ ...
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")
静态ip怎么设置objShell.ShellExecute "E:\d", "", "", "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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论