Windows批处理脚本修改主机名

系统教程 guchen 25℃ 0评论

以下Windows批处理脚本可以实现更改计算机名字的作用,通过网上搜集资料整合完成,已调试成功,可以在Windows XP, Server 2003, Server 2008, Win7, Win10环境下成功执行脚本(已测试)。

如果有专业的朋友,欢迎给出合理建议。

代码如下:

:: ----------------------------------------------------------------------------
:: hostName.bat
:: Batch program to modify the computer name
:: By: Gu chen
:: Windows version: XP, Server 2003, Server 2008, Win7, Win10
:: ----------------------------------------------------------------------------
@echo off
color 1F
title Modify the computer name


:Main
cls
echo.
echo The current computer name is: %computername%
set /p name=Please enter a computer name that needs to be modified:
set hostname=%computername%
set AllLetter=a b c d e f g h i j k l m n o p q r s t u v w x y z
set "code=A B C D E F G H I J K L N M O P Q R S T U V W X Y Z"
for %%i in (%code%) do call,set "strCapital=%%name:%%i=%%i%%"
::echo %strCapital%


if "%name%" == "!" goto rangehostname else goto Main
if "%name%" == "" goto nullhostname else goto Main
if /i "%name%" == "%hostname%" goto samehostname else goto Main
if /i not "%name%" == "%hostname%" goto modifyhostname
echo.
echo Input error, press any key to re-enter
set name=
pause>nul
goto Main


:nullhostname
echo.
echo Enter the computer name is empty, press any key to re-enter
set name=
pause>nul
goto Main


:samehostname
echo.
echo The current computer name is the same as the name of the input computer,
echo Press any key to re-enter
set name=
pause>nul
goto Main


:rangehostname
echo.
echo Only input numbers and alphabetical combinations [a-z/A-Z/0-9/-],
echo Press any key to re-enter
set name=
pause>nul
goto Main


:modifyhostname
setlocal enabledelayedexpansion
for /f "tokens=1* delims=abcdefghijklmnopqrstuvwxyz" %%a in ("a%name%a") do (
if "%%a.%%b" == "a.a" (
goto :RightInput
) else (
echo.
echo Only input numbers and alphabetical combinations [a-z/A-Z/0-9/-],
echo Press any key to re-enter
set name=
pause>nul
goto Main
)
)


:RightInput
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
reg add "HKEY_CURRENT_USER\Volatile Environment"
/v USERDOMAIN /t reg_sz /d %name% /f >nul 2>nul
reg add "HKEY_CURRENT_USER\Volatile Environment"
/v USERDOMAIN_ROAMINGPROFILE /t reg_sz /d %name% /f >nul 2>nul
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName"
/v ComputerName /t reg_sz /d %name% /f >nul 2>nul
echo.
echo Modify the computer name is completed, press any key to restart the system
set name=
pause>nul
shutdown /r /f /t 0

转载请注明:顾辰的技术博客 » Windows批处理脚本修改主机名

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址