' This program is written to find the existing computername of a Windows Computer, display it to the user, and then ask the user for a new computername.
' Then the script changes the computername to the one entered by the user and reboots Windows.
'we define our variables here
option explicit
dim strComputerName
dim wshShell
dim vInput
dim vResult
dim sReg1
dim sReg2
dim vInputU
dim vMsg
'These are the registry keys used to change the computername later on
sReg1 = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"
sReg2 = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\ParameterS\"
'These lines get the computername from Windows
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'And here we get the computername registered in DNS
'Here we ask the user for the new computername
vInput = InputBox("The current computer name in Windows is " & strComputerName & ". Please enter a new name:","Computer Name")
'If the user enters "Cancel"
If vInput = -1 Or vInput = "" Then
vMsg = MsgBox ("You have canceled operation.", 16, "Good Bye") 'this message appears
Else ' IF the user did not cancel
vResult = MsgBox("You have entered " & vInput & ". Is this correct?", 292, "Computer Name") 'We verify what the user typed
If vResult = vbNo Then 'if the typed computername is not verified by the user
vInput = InputBox("The current computer name is " & strComputerName & ". Please enter a new name:","Computer Name") 'we ask for the user's input again
If vInput = -1 Or vInput = "" Then ' and if the user clicks "cancel" this time
vMsg = MsgBox ("You have canceled operation.", 16, "Good Bye") 'this message appears
Else ' The user clicks "OK"
vResult = MsgBox("You have entered " & vInput & ". Is this correct?", 292, "Computer Name") 'we ask for the user to verify again
If vResult = vbNo Then 'if the user does not verify again
vMsg = MsgBox ("Sorry. Try running the program again.", 48, "Good Bye") 'then the program exits with this message
End If
End If
'at this point vResult should now be 6 if verified above and the following else clause should kick in
Else 'if the typed computername is then verified by the user
If vResult <> "" Then 'and if we don't have a null computername
vInputU = UCase(vInput) 'we put the new computername into upper case
wshShell.RegWrite sReg1 & "ComputerName", vInputU ' and modify the appropriate keys in the registry
wshShell.RegWrite sReg2 & "NV Hostname", vInputU
vMsg = MsgBox ("The computer name has changed. Would you like to restart?", 36, "Success!") 'this tells you the naming was successful and asks if you want to restart
If vMsg = vbYes Then ' if the user wants to restart and clicks "Yes"
wshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0" 'then the computer restarts
End If
Else ' if we have a blank username entered
MsgBox "Invalid input. Run program again." 'this message appears
End If
End If
End If
After using this script in the real world, I have found a major flaw. Apparently the computername needs to be restricted to the 15 character limit for NETBIOS. This script will force a longer name into the registry, and then your computer will no longer boot with an error with "lsass.exe".
ReplyDeleteAt some point I will be working to fix this, but for now, I hope this is helpful in pointing folks in the right direction.