Rename Machine Name with VBS

So… we renamed a user name with VBS here – Linky but now we need to change the machine name.
How simple is that? very

As you can see below specify a machine name under
strNewName = “InRimServer”
So “InRimServer” is the hard coded name of the script but you can change this with a MSGBOX or pull the value from other locations….
And initiate a reboot in the end of the script (or manually afterwards)



'----------------------------------------------------------------------

	const HKLM   = &H80000002
	
	Set WshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set oNetwork = CreateObject("WScript.Network")
	
	strCurrentComputerName = oNetwork.ComputerName
 	
 	strNewName = "InRimServer"
 	
	strKeyPath   = "System\CurrentControlSet\Control\ComputerName\ComputerName"
	set objReg = GetObject("winmgmts:\\" & strCurrentComputerName & "\root\default:StdRegProv")
	intRC = objReg.SetStringValue(HKLM, strKeyPath, "ComputerName", strNewName)
	
	if intRC <> 0 Then
		MSGBOX("Error setting ComputerName value: " & intRC)
	Else
		MSGBOX("Successfully set ComputerName value to " & strNewName)
	end if
	
	strKeyPath   = "System\CurrentControlSet\Services\Tcpip\Parameters"
	intRC = objReg.SetStringValue(HKLM, strKeyPath, "NV Hostname", strNewName)
	if intRC <> 0 Then
		MsgBox("Error setting NV Hostname value: " & intRC)
	Else
		MsgBox("Successfully set NV Hostname value to " & strNewName)
	end if
 	
 	'Remove the comments if you want an auto reboot
' 	MsgBox("Rebooting system...")
' 	WScript.Echo "Rebooting system..."
'	Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}//" & strComputer).ExecQuery("select * from Win32_OperatingSystem where Primary=true")
'    for each OpSys in OpSysSet
'        OpSys.Reboot()
'    Next    

This entry was posted in Scripting and tagged , . Bookmark the permalink.
  • http://www.bing.com/ Randhil

    YMMD with that aesnwr! TX