Windows 管理员权限开机静默执行读取文件配置IP

1.获取管理员权限:
:获取管理员权限
%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,"/c %~s0 ::","",“runas”,1) (window.close)&&exit
:保持当前目录下运行
cd /d “%~dp0”

2.开机运行—添加开机运行注册表
开机运行startdrv.vbs,在Run key中添加一项StartUsbDrv 其值为windows\system32\下的startdrv.vbs.
WriteRegStr HKLM “Software\Microsoft\Windows\CurrentVersion\Run” “StartUsbDrv” “$WINDIR\System32\StartDrv.vbs”

3.静默执行windows的批处理文件
通过vbs静默执行set.bat set.bat在windows\system32\目录下
set ws=WScript.CreateObject(“WScript.shell”)
ws.Run “C:\Windows\System32\set.bat /start”, 0, True
0:静默隐藏cmd窗口 True:等命令执行完在执行下一个命令

4.批处理中借助vbs实现延时:
:设置延时时间
echo WScript.sleep 10000 > sleep.vbs
Wscript sleep.vbs

6.获取本地网卡名称(本地所有网卡的名称都能获取出来)
setlocal enabledelayedexpansion
set m=0
for /f “tokens=1* delims=,” %%a in (‘Getmac /v /nh /fo csv’) do (
set /a m+=1
set “name!m!=%%a”
set “name=%name:~1,-1%”
)

7.读取D盘vm_network.ini文件获得ip配置并设置网络实例
@echo off & setlocal enabledelayedexpansion
:延时等待D盘挂载
echo WScript.sleep 10000 > sleep.vbs

:获取管理员权限
%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,"/c %~s0 ::","",“runas”,1)(window.close)&&exit
:保持当前目录下运行
cd /d “%~dp0”

for /f “delims=” %%a in (‘netsh interface show interface^|find “连接”’) do (
set “str=%%a”
for /f “tokens=*” %%b in ("!str:~46!") do (
set myname=%%b
)
)
set times=3
echo %times%
pause
for /l %%i in (1,1,%times%) do (
pause
if not exist D:\vm_network.ini (
echo “file not exist”
Wscript sleep.vbs
)
)

for /f “tokens=1,2 delims==:” %%a in (D:\vm_network.ini) do (
if “%%a”“dhcp” set dhcp=%%b
if “%%a”
“ip” set ip=%%b
if “%%a”“submask” set submask=%%b
if “%%a”
“gateway” set gateway=%%b
if “%%a”“auto_dns” set auto_dns=%%b
if “%%a”
“main_dns” set main_dns=%%b
if “%%a”==“back_dns” set back_dns=%%b

)

del /F D:\vm_network.ini

if %dhcp%== true goto ip_dhcp
if %dhcp%== false goto ip_static

:ip_dhcp
echo “set ip dhcp”
netsh interface ip set address name="%myname%" source=dhcp
if %auto_dns%== true goto dns_dhcp
if %auto_dns%== false goto dns_static
exit

:ip_static
echo “set ip static”
netsh interface IP set address “%myname%” static %ip% %submask%
netsh interface ip add address “%myname%” gateway=%gateway% gwmetric=1
if %auto_dns%== true goto dns_dhcp
if %auto_dns%== false goto dns_static
exit

:dns_dhcp
netsh interface ip set dns name="%myname%" source=dhcp
exit

:dns_static
netsh interface IP set dns “%myname%” static %main_dns%
netsh interface ip add dns “%myname%” %back_dns% index=2
exit

你可能感兴趣的:(技术,network,windows平台)