VB Script to prompt user to enter computer name without rebooting

Answered VB Script to prompt user to enter computer name without rebooting

  • Thursday, January 24, 2013 7:24 PM
     
     

    I am new to VBscripting although I have some syntax knowledge. I have just created a windows AIK image and in my answer file I would like to run 3 scripts on first log on.

    1st I would like a script to Prompt the user to input a computer name without rebooting.

    2nd I have a script to join the computer to the domain

    3rd I have a script to create shortcuts on all users desktops.

    I have already compiled the 2nd and 3rd scripts but I am having ungodly amount of trouble with the 1st script. I have spent hours online searching for a vbscript to do this, I had one but it forced a reboot and that will cancel out the other 2 scripts I need to run in sequence. I need this to run in a .vbs format so that all I have to do is place the script file in the C:\windows\Setup\Scripts folder so that it will run.

    We have a naming convention at the company here based on desktops (PWDAXXX) and laptops (NWDAXXX) so I need the user to input this at first boot before joing to the domain so that AD doesn't over populate with erroneous names. We are running Windows 7 only.

    Thanks in advanced for your help.

All Replies

  • Thursday, January 24, 2013 7:44 PM
     
     
    Setting a computer name and joining a domain will require a reboot, regardless of the scripting language you use. It's the same when you use the GUI via the Control Panel.
  • Thursday, January 24, 2013 7:52 PM
     
     
    Setting a computer name and joining a domain will require a reboot, regardless of the scripting language you use. It's the same when you use the GUI via the Control Panel.
    I understand that it will require a reboot to take effect although, I have had scripts (I wish I did not lose them) that applied the change and when you click on computer>properties it says Computer Name: XXXX (will be renamed to XXXXXXX After reboot) so that when my 2nd script runs it will join the computer to the domain with that name.
  • Thursday, January 24, 2013 7:58 PM
     
     

    Are you asking for someone to write a script for you?

    Try this: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 8:05 PM
     
     

    Are you asking for someone to write a script for you?


    ¯\_(ツ)_/¯

    Well I guess i deserved that. No here is my script. 

    const HKLM   = &H80000002

    Set WshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set oNetwork = CreateObject("WScript.Network")

    strCurrentComputerName = oNetwork.ComputerName

    strNewName = INPUTBOX("Please Input a Computer Name")

    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

    It says "Successfully set computer name to XXXX" but when I go to the system properties it stills hows the same computer name. Usually when i ran this before, it said i.e. Computer Name: PWDA700 (will rename to PWDA701 after system reboot).

      
  • Thursday, January 24, 2013 8:10 PM
     
     

    That is not how we rename a computer.  WMI has a rename method to do this:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa393056(v=vs.85).aspx


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 8:24 PM
     
     
    I have tried that script also, it returns a value Rename Failed. Error=0 I understand that if a value of 0 is returned that it modified the value. when i check in system properties it does not say that. when i reboot the computer name remains the same.
  • Thursday, January 24, 2013 8:33 PM
    Moderator
     
     

    Are you running the script from an elevated session (right-click, Run as administrator)?

    Bill

  • Thursday, January 24, 2013 9:03 PM
     
     

    Yes I have ran that same script with local admin credentials as well as Domain admin credentials. Ive ran it with virtual machine and it does not accept the computer name that the user inputs.

    I had a script a while ago that is the same as the above mentioned script that used to work although I have altered it because I was attempting to get a single script to run at first log on but it failed so when I tried to revert the changes the script broke. I apologize if I am coming off as frustrated although I have spent more then a necessary amount of time to develop a simple basic script that will change a computer name at first login. The only other way that I can do this is, in my answer file delete the xml code <computername> and let windows prompt me for that. It seems that will have to do.

    If anyone does come up with a script to do this I will greatly appreciate it as well though.

  • Thursday, January 24, 2013 9:05 PM
    Moderator
     
     
    Yes I have ran that same script with local admin credentials as well as Domain admin credentials.

    That is not the same as running elevated. A script that changes the computer name must run with elevated privileges. You must explicitly right-click the command prompt (or PowerShell) shortcut and choose "Run as administrator." Then you must run your script from inside that elevated window.

    Bill

  • Thursday, January 24, 2013 9:08 PM
     
     

    You cannot change the name of a domain joined machine by editing the registry.

    You cannot see the new machine name without a reboot.  You are asking explicitly torename a machine without a reboot.  That cannot be done.


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 9:21 PM
     
      Has Code

    Another thing you might be doing is trying to rename the local machine with alternate credentials.  It is NOT possible to rename the local machine with credentials.

    You cannot use this construct on the local machine:

    Name = "name"
    Password = "password"
    Username = "username"
    
    
    Set objWMIService = GetObject("Winmgmts:root\cimv2")
    
    ' Call always gets only one Win32_ComputerSystem object.
    For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
    
            Return = objComputer.rename(Name,Password,Username)
            If Return <> 0 Then
               WScript.Echo "Rename failed. Error = " & Err.Number
            Else
               WScript.Echo "Rename succeeded." & _
                   " Reboot for new name to go into effect"
            End If
    
    Next
    

    You must use this version for the local machine:

    Name="newname"
    Set objWMIService = GetObject("Winmgmts:root\cimv2")
    
    ' Call always gets only one Win32_ComputerSystem object.
    For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
    
            Return = objComputer.rename(Name)
            If Return <> 0 Then
               WScript.Echo "Rename failed. Error = " & Err.Number
            Else
               WScript.Echo "Rename succeeded." & _
                   " Reboot for new name to go into effect"
            End If
    
    Next
    

    Note that there is no username and password argument.  See the link to the documentation that I posted.  It is very clear about this.

    Once again, you cannot rename a domain machine by editing the registry.


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 9:27 PM
     
     

    Yes I have ran that same script with local admin credentials as well as Domain admin credentials.

    That is not the same as running elevated. A script that changes the computer name must run with elevated privileges. You must explicitly right-click the command prompt (or PowerShell) shortcut and choose "Run as administrator." Then you must run your script from inside that elevated window.

    Bill

    I apologize. I have done that by clicking run as admin on the script. both with a computer that is not joined to the domain, as well as a computer that is on the domain with my account (domain admin) so I do not believe it is a permissions issue. I believe it is a scripting issue although I am not that advanced yet to troubleshoot this.

    You cannot change the name of a domain joined machine by editing the registry.

    You cannot see the new machine name without a reboot.  You are asking explicitly torename a machine without a reboot.  That cannot be done.


    ¯\_(ツ)_/¯

    First off, this is the 4th time that you have not provided me with any information except with "This cannot be done" or "you are not doing it right" I have done this. My above mentioned script accomplished this task with minimal effort. the computer name took. I would post a screenshot of this although I have just created an account with this forum and my account needs to be verified before I can post pics or links. All I am looking for is a script to do what you can do by right clicking on computer and clicking properties then going to change settings then clicking change and typing a computer name there. It will change after reboot but while under that state if I were to join it to the domain while it says (will change to XXXXXX after restarting this computer) AD will take that computer name that I have given.
  • Thursday, January 24, 2013 9:37 PM
     
      Has Code

    Another thing you might be doing is trying to rename the local machine with alternate credentials.  It is NOT possible to rename the local machine with credentials.

    You cannot use this construct on the local machine:

    Name = "name"
    Password = "password"
    Username = "username"
    
    
    Set objWMIService = GetObject("Winmgmts:root\cimv2")
    
    ' Call always gets only one Win32_ComputerSystem object.
    For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
    
            Return = objComputer.rename(Name,Password,Username)
            If Return <> 0 Then
               WScript.Echo "Rename failed. Error = " & Err.Number
            Else
               WScript.Echo "Rename succeeded." & _
                   " Reboot for new name to go into effect"
            End If
    
    Next

    You must use this version for the local machine:

    Name="newname"
    Set objWMIService = GetObject("Winmgmts:root\cimv2")
    
    ' Call always gets only one Win32_ComputerSystem object.
    For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")
    
            Return = objComputer.rename(Name)
            If Return <> 0 Then
               WScript.Echo "Rename failed. Error = " & Err.Number
            Else
               WScript.Echo "Rename succeeded." & _
                   " Reboot for new name to go into effect"
            End If
    
    Next

    Note that there is no username and password argument.  See the link to the documentation that I posted.  It is very clear about this.

    Once again, you cannot rename a domain machine by editing the registry.


    ¯\_(ツ)_/¯

    Ran this same exact script verbatim. Came up with the same error. 0 checked the system properties, looked at the computer name and it still shows the same computer name. Rebooted the computer, Still shows the same computer name. This script is not working. Ran it as administrator by right clicking and clicking run as administrator on both a local machine and a machine on the domain. both times, it did not work. So whats the explanation this time? Should I reimage my machine? Should I update group policy on the domain machine? should I just go on another forum and see what they say? 
  • Thursday, January 24, 2013 10:51 PM
     
     
    Ran this same exact script verbatim. Came up with the same error. 0 checked the system properties, looked at the computer name and it still shows the same computer name. Rebooted the computer, Still shows the same computer name. This script is not working. Ran it as administrator by right clicking and clicking run as administrator on both a local machine and a machine on the domain. both times, it did not work. So whats the explanation this time? Should I reimage my machine? Should I update group policy on the domain machine? should I just go on another forum and see what they say? 

    Sorry but we cannot trouble shoot your system for you.  You will have to do that.

    I agree that you are doing something wrong in your script but you still have not posted your script that changes the name of the machine using WMI.  I suspect you have an error in that script but since you won't post it we cannot tell.

    Note that cxhangung the machine namne in the registry in a adomain may not always work as expected.  If for some reason teh DC vcannot be contacted then the machine will likely fall out of the domian.  Uising the 'rename' API should prevent this.

     


    ¯\_(ツ)_/¯

  • Thursday, January 24, 2013 11:36 PM
     
     Answered Has Code

    Here is the exact script that I have just run on XP, Windows 7 and Windows 8.  It works in all cases in or out of a domain.  I modified the above script to trap the return code and report it if not zero.

    I have also added a message prompting the user to reboot to make the name visible.  The original Microsoft code (above) is misleading because it attempts to display the new name before the reboot.  No matter how you change the name it will not be visible until after a reboot.  This has always been true.

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colComputers = objWMIService.ExecQuery _
        ("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputers
        errReturn = ObjComputer.Rename("W8Test")
        if errReturn <> 0 THen 
           msgbox "FAILED" & errReturn
        end if
        WScript.Echo "You must reboot to finishing changing the name."
    Next
    


    ¯\_(ツ)_/¯