从命令行更改Windows主机名

时间:2020-03-05 18:50:59  来源:igfitidea点击:

是否可以使用现成的工具从命令行更改Windows 2003中的主机名?

解决方案

回答

我不知道执行此操作的命令,但是我们可以在VBScript或者类似工具中执行此操作。
有点像:

sNewName = "put new name here" 

Set oShell = CreateObject ("WSCript.shell" ) 

sCCS = "HKLM\SYSTEM\CurrentControlSet\" 
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\" 
sCompNameRegPath = sCCS & "Control\ComputerName\" 

With oShell 
.RegDelete sTcpipParamsRegPath & "Hostname" 
.RegDelete sTcpipParamsRegPath & "NV Hostname" 

.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName 
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName 
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName 
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName 
End With ' oShell 

MsgBox "Computer name changed, please reboot your computer"

原版的

回答

可以使用netdom.exe命令行程序。可从Windows XP支持工具或者Server 2003支持工具(均在安装CD上)获得。

这里的使用准则

回答

这是使用WHS脚本执行此操作的另一种方法:

Set objWMIService = GetObject("Winmgmts:root\cimv2")

For Each objComputer in _
    objWMIService.InstancesOf("Win32_ComputerSystem")

    objComputer.rename "NewComputerName", NULL, NULL 
Next

来源

回答

前面提到的" wmic"命令是可以使用的方法,因为它默认安装在最新版本的Windows中。

这是我通过从环境中检索当前名称来进行概括的小改进:

wmic computersystem where name="%COMPUTERNAME%" 
     call rename name="NEW-NAME"

注意:该命令必须在一行中给出,但是我将其分成两部分以使不需要滚动。正如@rbeede提到的那样,我们必须重新启动才能完成更新。