免重启命令行下修改计算机主机名

作者: admin 分类: 未分类 发布时间: 2015-05-24 18:33

使用方法:
命令行下执行:

modfif.bat 你要设置的名字
@echo off
setlocal EnableDelayedExpansion
set newName=%1
echo [*] Current ComputerName is: %computername%
if not defined newName (
echo The syntax of the command is incorrect.
echo.
echo Help:
echo       %~n0 ComputerName
echo.
echo Example:
echo       %~n0 lostwolf-pc 
goto :EOF 
)


echo [*] Modifying registry keys...
::通过注册表修改本地机器名
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName /v ComputerName /d %newName% -f  >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\ComputerName\ComputerName /v ComputerName /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName /v ComputerName /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v Hostname /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters /v "NV Hostname" /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters /v Hostname /d %newName% -f >nul 2>nul
reg add HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters /v "NV Hostname" /d %newName% -f >nul 2>nul
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"  /v DefaultDomainName /t REG_SZ  /d %newName% /f >nul 2>nul
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"  /v AltDefaultDomainName /t REG_SZ  /d %newName% /f >nul 2>nul


echo [*] Finish updating registry...

::更新DNS
echo [+] Update DNS...
ipconfig /renew >nul 2>nul
ipconfig /flushdns >nul 2>nul
echo [*] Finish updating DNS...
set        computername=%newName%
echo [+] Modified successfully.
echo.
echo [+] NewName: %newName%

非交互shell 下 bat

还有更简单的方法

Wmic computersystem where "name='%computername%'" call rename "new name"

发表评论

标签云