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

转载于:https://www.cnblogs.com/feiquan/p/10533506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值