bat 实现主机hostname的修改


主机实现hostname的修改原理:

  修改注册表中的值:

    hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName 下的 ComputerName

    hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName 下的 ComputerName

    hklm\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters  下的 Hostname

    hklm\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters 下的 NV Hostname

 

bat脚本实现:

@echo off
if not "%OS%"=="Windows_NT" goto END
if "%1"=="" (
    set INFO=echo &&set skip=0&&set err=0
) else if "%1"=="/?" (
    set INFO=echo && set skip=1&&set err=0&&goto DESC
) else  if "%1"=="-q" (
    set INFO=echo &&set err=1&&goto ERR
) else  if "%1"=="-r" (
    set INFO=echo &&set err=1&&goto ERR
) else (
    set name=%1 && goto CHANGE
)

%INFO% *******************************
%INFO% Script:ChangeHostname.bat
%INFO% Creation Date : 2019/3/14 16:13
%INFO% Last Modified : 2019/3/14 20:49 
%INFO% Author: feiquan
%INFO% Email: 2283320260@qq.com
:DESC
%INFO% *********************************
%INFO% Description: 
%INFO%        chang you hostname
%INFO% *********************************
%INFO% Examples:
%INFO%         ChangeHostname.bat  ===^> Interact mode
%INFO%         ChangeHostname.bat  /? ===^> Help mode
%INFO%         ChangeHostname.bat  "NewHostname" ===^> Commandline mode
%INFO%         ChangeHostname.bat  "NewHostname"  -q ===^> Quiet mode
%INFO%         ChangeHostname.bat  "NewHostname"  -r ===^> Restart Sysetm After 60s
%INFO%         ChangeHostname.bat  "NewHostname"  -q -r ===^>Quiet mode, Restart Sysetm After 60s
%INFO%         Echo N ^| ChangeHostname.bat "NewHostname" -q ===^>Quiet mode,  No Restart Sysetm
%INFO% *********************************
if "%skip%"=="1" goto END


:START
color 07

title Change Hostname
setlocal EnableDelayedExpansion
for /F "skip=1 tokens=3" %%i in ('reg query "hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" -v ComputerName' ) do set nowHostname=%%i

echo Your computer hostname : %nowHostname%

set /p n=Please input new hostname :
set name="!n!"
echo.

:CHANGE
if "%2"=="-q" goto QUIET
if "%3"=="-q" goto QUIET

set /p sure=Do you want to change hostname %name% ?(Y/N)
if  /i not "%sure%"=="y" goto END

:QUIET
reg add "hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" -v ComputerName -t REG_SZ -d %name% -f

reg add "hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" -v ComputerName -t REG_SZ -d %name% -f

reg add "hklm\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -v Hostname -t REG_SZ -d %name% -f

reg add "hklm\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -v "NV Hostname" -t REG_SZ -d %name% -f

echo.
for /F "skip=1 tokens=3" %%i in ('reg query "hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" -v ComputerName' ) do set nowHostname=%%i

echo Your computer hostname has changed : %nowHostname%
echo  After 60 seconds , Restart your system....

if "%2"=="-r" goto RESERT
if "%3"=="-r" goto RESERT
set /p restart=Do you want to restart your system after 60s ? (Y/N)
if  /i not "%restart%"=="y" goto END
:RESERT
shutdown -r -t 60 -c "Your computer hostname has changed : %nowHostname% . Restart your system 60s ...."  -d p:2:4
goto END

:ERR
echo Format Error , Please with "ChangeHostname.bat  /? " to learn more ....

:END
echo exiting....

 

下载地址:

  链接:https://pan.baidu.com/s/1Ik3C0juf4um1hU58p9sj9A
  提取码:p28f


作者:feiquan,发布于:2019/03/15
原文:https://www.cnblogs.com/feiquan/p/10533506.html