复制代码
- :name
- set /p cname=请输入计算机名:
- echo REGEDIT4 >c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName] >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\ComputerName\ComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] >> c:\windows\reg.reg
- echo "NV Hostname"="%cname%" >> c:\windows\reg.reg
- echo "Hostname"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_USERS\S-1-5-18\Software\Microsoft\Windows\ShellNoRoam] >> c:\windows\reg.reg
- echo @="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters] >> c:\windows\reg.reg
- echo "NV Hostname"="%cname%" >> c:\windows\reg.reg
- echo "Hostname"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> c:\windows\reg.reg
- echo "DefaultDomainName"="%cname%" >> c:\windows\reg.reg
- echo "AltDefaultDomainName"="%cname%" >> c:\windows\reg.reg
- regedit /s c:\windows\reg.reg
- cmd
复制代码
- @echo off
- set /p name=请输您的计算机名:
- reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v ComputerName /t reg_sz /d %name% /f >nul 2>nul
- reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /t reg_sz /d %name% /f >nul 2>nul
- reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname /t reg_sz /d %name% /f >nul 2>nul
- echo.
- echo 修改计算机名完毕
- pause&exit
复制代码
- @echo off
- ::1.设置环境变量
- set MASK=255.255.255.0
- set GATEWAY=192.168.31.1
- set DNS1=114.114.114.114
- set DNS2=114.114.114.110
- set WINS=192.168.31.1
- set run_log=%temp%\run_log.txt
- ::2.运行环境检查
- echo. >%run_log%
- echo. >>%run_log%
- echo ======================================>>%run_log%
- echo 运行时间 %date% %time% >>%run_log%
- echo ======================================>>%run_log%
- del %temp%\mac.txt >nul 2>nul
- del %temp%\a.txt >nul 2>nul
- wmic nic >nul 2>nul
- wmic nic >nul 2>nul >%temp%\a.txt && (find /i "MACAddress" %temp%\a.txt >nul 2>nul || echo ************运行权限不够,请以管理员运行***************** >>%run_log% && start %run_log% &&exit)
- ::3.获取本机所有MAC,地址,格式为 XX-XX-XX-XX-XX-XX
- wmic nic get MACAddress >%temp%\name.txt && for /f "skip=1" %%i in ('type %temp%\name.txt') do for /f "tokens=1-6 delims=:" %%I in ("%%i") do echo %%I-%%J-%%K-%%L-%%M-%%N>>%temp%\mac.txt
- ::4.将获取到的本机MAC地址逐一到本批处理后面的列表去查找,匹配的通常是本地连接的MAC连接,将匹配的MAC地址保存下来。
- :: 注1:这种方式可以应对电脑上安装虚拟机,或者机器是win7及以上的系统,会有多个MAC。
- :: 注2:至少要有一个匹配MAC,如果找不到任何匹配MAC,则报错退出。这可以避免在没有收集MAC的机器上运行时,错误修改原来的信息。
- del %temp%\exist-mac.txt >nul 2>nul
- for /f %%i in (%temp%\mac.txt) do find /i "%%i" %0 >nul 2>nul && echo %%i>%temp%\exist-mac.txt
- if exist %temp%\exist-mac.txt (for /f %%i in (%temp%\exist-mac.txt) do set MAC=%%i) else (echo ************未找到匹配的MAC项,中断批处理执行************ >>%run_log% &&start %run_log% &&exit)
- ::5.获取找到MAC地址对应的网络连接名称,通常是本地连接,
- :: 注1:对于一些多网卡环境,如机器装有多块网卡,会生成本地连接 2的名称, 注意:2前面有个空格,
- :: 注2:还有一种情况,本地连接的名称被改,在学校机房,干这种事情的人大有人在
- for /f "tokens=1-6 delims=-" %%i in ("%MAC%") do set MAC1=%%i:%%j:%%k:%%l:%%m:%%n
- wmic nic where MACaddress="%MAC1%" get NetConnectionID >%temp%\mac1.txt && for /f "tokens=* skip=1" %%i in ('type %temp%\mac1.txt') do if NOT "%%i"=="" set NetConnectionID=%%i
- :intercept
- if "%NetConnectionID:~-1%"==" " set "NetConnectionID=%NetConnectionID:~0,-1%"&goto intercept
- ::6.获取预设的计算机名\IP地址\计算机描述信息
- for /f "tokens=1-4" %%i in ('more /e +10 %0 ^|find /i "%mac%"') do set name=%%i&& set IP=%%j&& set desc=%%l
- if "%name%"=="" (echo ********************匹配MAC预设计算机信息不存在,本程序将退出******************** >>%run_log% &&start %run_log% &&exit)
- if "%ip%"=="" (echo ********************匹配MAC预设IP地址信息不存在,本程序将退出******************** >>%run_log% &&start %run_log% &&exit)
- if "%desc%"=="" (echo ********************匹配MAC预设计算机描述信息不存在,本程序将退出****************** >>%run_log% &&start %run_log% &&exit)
- for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\lanmanserver\parameters" /v srvcomment 2^>nul') do set current_desc1=%%k
- for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters" /v srvcomment 2^>nul') do set current_desc2=%%k
- wmic nicconfig where MACaddress="%MAC1%" get IPaddress>%temp%\a.txt && for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set current_ip=%%~i
- wmic nicconfig where MACaddress="%MAC1%" get IPSubnet>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set IPSubnet=%%~i
- wmic nicconfig where MACaddress="%MAC1%" get DefaultIPGateway>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set DefaultIPGateway=%%~i
- wmic nicconfig where MACaddress="%MAC1%" get DNSServerSearchOrder >%temp%\a.txt && for /f "tokens=1-8 skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set current_DNS1=%%~i& set current_DNS2=%%~j& set DNS3=%%~k& set DNS4=%%~l
- wmic nicconfig where MACaddress="%MAC1%" get WINSPrimaryServer>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set WINSPrimaryServer=%%i
- echo. **************************信息汇总********************************
- echo 网络连接名称 【%NetConnectionID%】
- echo MAC地址为 【%mac%】
- echo 当前IP地址 【%current_ip%】 预设IP地址 【%IP%】
- echo 当前子网掩码 【%IPSubnet%】 预设子网掩码 【%MASK%】
- echo 当前默认网关 【%DefaultIPGateway%】 预设默认网关 【%GATEWAY%】
- echo 当前DNS1 【%current_DNS1%】 预设DNS1 【%DNS1%】
- echo 当前DNS2 【%current_DNS2%】 预设DNS2 【%DNS2%】
- echo 当前WINS服务器 【%WINSPrimaryServer%】 预设WINS服务器 【%WINS%】
- echo.
- echo 当前计算机名 【%current_name1%】 预设计算机名 【%name%】
- echo 当前描述 【%current_desc1%】 预设计算机描述 【%desc%】
- echo. *********************************************************************
- echo.
- ::7.执行判断、修改计算机名\IP地址\计算机描述信息
- if "%current_desc1%"=="%desc%" (echo 计算机注释1符合要求,跳过修改>>%run_log% ) else (echo 正在修改计算机注释1 && reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\lanmanserver\parameters" /v srvcomment /t reg_sz /d %desc% /f >nul 2>nul && echo 修改计算机注释1成功 >>%run_log% || echo ********************修改计算机注释1失败 ******************** >>%run_log%)
- if "%current_desc2%"=="%desc%" (echo 计算机注释2符合要求,跳过修改>>%run_log%) else (echo 正在修改计算机注释2 && reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters" /v srvcomment /t reg_sz /d %desc% /f >nul 2>nul && echo 修改计算机注释2成功>>%run_log% || echo ********************修改计算机注释2失败 ******************** >>%run_log%)
- if "%name%"=="%computername%" (
- >>%run_log% echo 计算机名符合要求,跳过修改
- ) else (
- echo 正在修改计算机名
- REM wmic computersystem where "name='%computername%'" call rename %name%
- call :ChangeName
- if errorlevel 1 (
- >>%run_log% echo ********************修改计算机名失败 ********************
- ) else (
- >>%run_log%echo 修改计算机名成功
- )
- )
- if "%current_ip%"=="%IP%" (echo IP地址符合要求,跳过修改>>%run_log%) else (set change_pro=IP地址)
- if "%IPSubnet%"=="%MASK%" (echo 子网掩码符合要求,跳过修改>>%run_log%) else (set change_pro=%change_pro%、子网掩码)
- if "%DefaultIPGateway%"=="%GATEWAY%" (echo 默认网关地址符合要求,跳过修改>>%run_log%) else (set change_pro=%change_pro%、默认网关)
- if "%change_pro%"=="" (echo IP地址\子网掩码\默认网关符合要求,跳过修改>>%run_log%) else (echo 正在修改%change_pro% && netsh interface ip set address "%NetConnectionID%" static %IP% %Mask% %GATEWAY% 1 >nul 2>nul && echo 修改%change_pro%成功 >>%run_log% || echo ********************修改%change_pro%失败 ******************** >>%run_log%)
- if "%current_DNS1%"=="%DNS1%" (echo DNS1地址符合要求,跳过修改>>%run_log%) else (echo 正在修改DNS1地址 && netsh interface ip set dns "%NetConnectionID%" static %DNS1% register=PRIMARY >nul 2>nul && echo 修改DNS1地址成功 >>%run_log% || echo ********************修改DNS1地址失败 ******************** >>%run_log%)
- if "%current_DNS2%"=="%DNS2%" (echo DNS2地址符合要求,跳过修改>>%run_log%) else (echo 正在修改DNS2地址 && netsh interface ip add dns "%NetConnectionID%" %DNS2% index=2 >nul 2>nul && echo 修改DNS2地址成功 >>%run_log% || echo ********************修改DNS2地址失败 ******************** >>%run_log%)
- if "%WINSPrimaryServer%"=="%WINS%" (echo WINS地址符合要求,跳过修改>>%run_log%) else (echo 正在修改WINS地址 && netsh interface ip set wins "%NetConnectionID%" static %WINS% >nul 2>nul && echo 修改WINS地址成功 >>%run_log% || echo ********************修改WINS地址失败 ******************** >>%run_log%)
- start %run_log%
- goto :eof
- :ChangeName
- set cname=%name%
- echo REGEDIT4 >c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName] >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\ComputerName\ComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] >> c:\windows\reg.reg
- echo "NV Hostname"="%cname%" >> c:\windows\reg.reg
- echo "Hostname"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_USERS\S-1-5-18\Software\Microsoft\Windows\ShellNoRoam] >> c:\windows\reg.reg
- echo @="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName] >> c:\windows\reg.reg
- echo "ComputerName"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters] >> c:\windows\reg.reg
- echo "NV Hostname"="%cname%" >> c:\windows\reg.reg
- echo "Hostname"="%cname%" >> c:\windows\reg.reg
- echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> c:\windows\reg.reg
- echo "DefaultDomainName"="%cname%" >> c:\windows\reg.reg
- echo "AltDefaultDomainName"="%cname%" >> c:\windows\reg.reg
- regedit /s c:\windows\reg.reg
- goto :eof
- ::以下为预设信息区域
- 计算机名 新规划地址 MAC地址 计算机描述
- 12F-JF01-A002 192.168.12.102 54-04-a6-66-bd-bd 12楼机房A02
- 12F-JF01-A003 192.168.12.103 00-30-67-ba-0c-ac 12楼机房A03
- 12F-JF01-A004 192.168.12.104 00-e0-62-0b-1f-6c 12楼机房A04
- 12F-JF01-A005 192.168.12.105 bc-ae-c5-5d-65-c0 12楼机房A05
- 12F-JF01-A006 192.168.12.106 00-e0-66-02-29-5e 12楼机房A06
- 12F-JF01-A007 192.168.12.107 50-e5-49-be-c0-dc 12楼机房A07
- 12F-JF01-A008 192.168.12.108 00-e0-66-0d-71-d9 12楼机房A08
- 12F-JF01-A009 192.168.12.109 00-e0-66-02-dd-d8 12楼机房A09
复制代码
- Rename-Computer -NewName (Read-Host "Enter new computer name")#-Restart
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |