# WINDOWS批处理应用1-更改计算机参数

# 应用场景

这两天公司网络要进行整体规划,有一个需求是每台电脑要按照规划重命名电脑名,重设ip,我当时接到这个需求几近崩溃,后来一想,为什么不写个脚本每个电脑运行一下,好赖是减少点工作量,于是在实施前的一个下午研究了下windows脚本。

# 明确需求:

  • 重命名电脑名字,要求格式:每个部门标识名+自增数字,比如 YanFa_100
  • 重设ip,要求ip末位同电脑名字后面的自增数字一样,比如192.168.1.100

为了实现这两个需求,我先花了一段时间学习了下批处理的基本知识。这里我就先不科普基本知识,批处理关于入门知识的相关文章会在后边逐渐发布,这里只针对功能性的要求给出相应的代码,方便同学们拿来使用。

# 代码片段:

# 更改计算机名字

@echo off
set /p NewName=输入要更改成的名字:
echo 要将计算机名称更改成:%NewName%

WMIC computersystem where caption="%COMPUTERNAME%" rename %NewName%
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %NewName% /f

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v ComputerName /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname /t reg_sz /d %NewName% /f

echo ————————————————————更改计算机名称成功,请重启!—————————————
echo 已经更改为:
hostname
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 更改电脑ip,网关,dns,掩码

@echo off
set /p NewIp=要更改的ip:
set /p NewMask=掩码将要更改为:
set /p NewGateway=网关将要更改为:
set /p NewDns=DNS将要更改为:
set /p BackupDns=备用DNS:
FOR /F "tokens=2 delims==," %%i in ('wmic nic where "NetConnectionStatus=2 and (netconnectionid like "%%以太网%%" or netconnectionid like "%%本地连接%%")" get netconnectionid /VALUE') DO set lanAdapter=%%i
echo 正在更改适配器是:%lanAdapter% 适配器
echo Ip将要更改为:%NewIp%
echo 掩码将要更改为:%NewMask%
echo 网关将要更改为:%NewGateway%
echo DNS将要更改为:%NewDns%
echo 备用DNS:%BackupDns%

FOR /F "tokens=2 delims==," %%j in ('wmic nic where "netconnectionid="%lanAdapter%"" get MACAddress /VALUE') DO set macAddress=%%j

if "%lanAdapter%"=="" (
	echo ———————————————"获取适配器为空!更改失败!"————————————————
	pause
	exit
)else (
	netsh interface ip set address name="%lanAdapter%" source=static addr=%NewIp% mask=%NewMask% gateway=%NewGateway% gwmetric=0
	netsh interface ip set dns name="%lanAdapter%" source=static addr=%NewDns% register=PRIMARY
	netsh interface ip add dns "%lanAdapter%" %BackupDns%
	echo ————————————————网络更改成功!!!!!!————————————————
	netsh interface ip show config "%lanAdapter%"
	netsh interface ip show dnsservers "%lanAdapter%"
)
pause
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 直接更改电脑名字和ip等网络参数

@echo off
set suffix=70

set  NewName=Yanfa_%suffix%
WMIC computersystem where caption="%COMPUTERNAME%" rename %NewName%
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %NewName% /f

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v ComputerName /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname /t reg_sz /d %NewName% /f

echo 新的电脑名字:%COMPUTERNAME%

set  NewIp=10.0.100.%suffix%
set  NewMask=255.255.255.0
set  NewGateway=10.0.100.254
set  NewDns=10.0.99.1
set  BackupDns=10.0.99.2
FOR /F "tokens=2 delims==," %%i in ('wmic nic where "NetConnectionStatus=2 and (netconnectionid like "%%以太网%%" or netconnectionid like "%%本地连接%%")" get netconnectionid /VALUE') DO set lanAdapter=%%i
echo 正在更改适配器是:%lanAdapter% 适配器
echo Ip将要更改为:%NewIp%
echo 掩码将要更改为:%NewMask%
echo 网关将要更改为:%NewGateway%
echo DNS将要更改为:%NewDns%
echo 备用DNS:%BackupDns%

FOR /F "tokens=2 delims==," %%j in ('wmic nic where "netconnectionid="%lanAdapter%"" get MACAddress /VALUE') DO set macAddress=%%j

if "%lanAdapter%"=="" (
	echo ———————————————"获取适配器为空!更改失败!"————————————————
	pause
	exit
)else (
	netsh interface ip set address name="%lanAdapter%" source=static addr=%NewIp% mask=%NewMask% gateway=%NewGateway% gwmetric=0
	netsh interface ip set dns name="%lanAdapter%" source=static addr=%NewDns% register=PRIMARY
	netsh interface ip add dns "%lanAdapter%" %BackupDns%
	echo ————————————————网络更改成功!!!!!!————————————————
	netsh interface ip show config "%lanAdapter%"
	netsh interface ip show dnsservers "%lanAdapter%"
)

pause
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 更改电脑名字和ip,dns,网关并获取MAC地址

下面的脚本是真正完成我们开篇要提的需求,它需要在同级目录下有一个名为“num.txt”的文本文件才能运行,这个文本文件是一个数字,也就是电脑名和ip的自增部分。将这个程序和num.txt放在u盘,插在要更改的每台电脑上,运行下,num.txt中的数字会加1,拔下u盘插在下一个要运行的电脑上,如此便可自动更改每台电脑的名字和ip。同时,脚本会获取每台电脑的MAC地址并存在以当前数字命名的文本文件中,同学们如需在更改名字的同时要获取其他信息,可自行更改脚本。

@echo off

set ComputerNamePrefix=YanFa_
set IpPrefix=10.0.0.
set NewMask=255.255.255.0
set NewGateway=10.0.0.254
set NewDns=10.0.100.1
set BackupDns=10.0.100.2

::——————————————————以下代码不能更改!!!!!!————————————————
set ConfigFile=%~dp0"num.txt"
set /p CurrentNum=<%ConfigFile%
set MacFile=%~dp0%CurrentNum%".txt"
echo 当前的计算是第:%CurrentNum% 台
set NewName=%ComputerNamePrefix%%CurrentNum%
echo 要将计算机名称更改成:%NewName%
set NewIp=%IpPrefix%%CurrentNum%

WMIC computersystem where caption="%COMPUTERNAME%" rename %NewName%
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %NewName% /f

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v ComputerName /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /t reg_sz /d %NewName% /f
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname /t reg_sz /d %NewName% /f

echo ————————————————更改计算机名字成功!——————————————已经更改为:
hostname



FOR /F "tokens=2 delims==," %%i in ('wmic nic where "NetConnectionStatus=2 and (netconnectionid like "%%以太网%%" or netconnectionid like "%%本地连接%%")" get netconnectionid /VALUE') DO set lanAdapter=%%i
echo 正在更改适配器是:%lanAdapter%适配器
echo Ip将要更改为:%NewIp%
echo 掩码将要更改为:%NewMask%
echo 网关将要更改为:%NewGateway%
echo DNS将要更改为:%NewDns%
echo 备用DNS:%BackupDns%

FOR /F "tokens=2 delims==," %%j in ('wmic nic where "netconnectionid="%lanAdapter%"" get MACAddress /VALUE') DO set macAddress=%%j
>%MacFile% echo %macAddress%
set /a CurrentNum=%CurrentNum%+1
>%ConfigFile% echo %CurrentNum%


if "%lanAdapter%"=="" (
	echo "——————————————获取适配器为空!更改失败!——————————————————"
	pause
	exit
)else (
	netsh interface ip set address name="%lanAdapter%" source=static addr=%NewIp% mask=%NewMask% gateway=%NewGateway% gwmetric=0
	netsh interface ip set dns name="%lanAdapter%" source=static addr=%NewDns% register=PRIMARY
	netsh interface ip add dns "%lanAdapter%" %BackupDns%
	echo ———————————————网络更改成功!!!!!!————————————————
	netsh interface ip show config "%lanAdapter%"
	netsh interface ip show dnsservers "%lanAdapter%"
)


pause

显示更改的计算机信息
@echo off
echo ——————————————————计算机名字:————————————————————
hostname

echo ——————————————————网络信息:——————————————————————

FOR /F "tokens=2 delims==," %%i in ('wmic nic where "NetConnectionStatus=2 and (netconnectionid like "%%以太网%%" or netconnectionid like "%%本地连接%%")" get netconnectionid /VALUE') DO set lanAdapter=%%i

echo ————————正在更改的适配器:%lanAdapter%——————————————
netsh interface ip show config "%lanAdapter%"
netsh interface ip show dnsservers "%lanAdapter%"

FOR /F "tokens=2 delims==," %%j in ('wmic nic where "netconnectionid="%lanAdapter%"" get MACAddress /VALUE') DO set macAddress=%%j
echo %macAddress%
pause
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76