Friday, November 27, 2009

VBScript for changing a computer's name

I am working on some scripts that will help us check and make sure our computer names in Windows are consistent with our computer names in DNS. Included here is a script that just changes the name of a computer to one that you specify in an input box, after also telling you the current computername in Windows. Then once you put in the new name, it asks you if you want to restart so that the change takes affect, and then restarts your computer. This script was written for 32-bit Windows XP. I will admit that I got a lot of the ideas from books and the web, but pretty much wrote this myself. I hope this will be useful to some folks.  I also have to apologize for some of the formatting; some of the lines were longer for my code than the blog editor here can take. 



' 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









1 comment:

  1. 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".

    At some point I will be working to fix this, but for now, I hope this is helpful in pointing folks in the right direction.

    ReplyDelete