WINDOWS批处理应用1

本文是原创,如需转发请在转发开头贴上原文地址,谢谢!

个人博客:http://www.muyunsoft.com

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

首先明确一下我们的需求:

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

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

更改电脑名字:

@echo off
set /p NewName=输入要更改成的名字:
echo 要将计算机名称更改成:%NewName%
wmic computersystem where "PrimaryOwnerName='%username%'" call rename "%NewName%"
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

更改电脑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

这个脚本是更改Ip,掩码,网关,DNS,备用DNS。如果不需要改变需要输入和原来一样的值。

更改电脑名字和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 "PrimaryOwnerName='%username%'" call rename "%NewName%"
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

显示更改的计算机信息

@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

上边的脚本因为编写匆忙未有足够测试,如果有bug或运行错误请评论发给我!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值