Announcement

Collapse
No announcement yet.

Script to Change PC Name to Serial Number

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to Change PC Name to Serial Number

    Hi All,

    I'm recently inherited a very broken IT infrastructure supporting about 200 employees and 400 machines and am looking for some insight into different things.

    First off, there is no AD here (I KNOW!) and no centralized management.

    One problem is that when they deployed all of the machines they used a CloneZilla image, but neglected to rename any of the computers, so almost every computer has a naming conflict on the network.

    I am looking for a script that will rename the computer name in Windows XP to the Serial Number of the computer (All Identical Lenovo T400).

    I have constructed the following VBscript from the little that I know / can find, but I'm not sure how to take the data I've gathered and change the pc name.

    Code:
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    
    
    Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
    
    For each objBIOS in colBIOS
      WScript.Echo "SERIAL=" & objBIOS.SerialNumber
    Next
    I know how to change the PC name with the command prompt, but alas not in VB.


    If anyone could help or provide any better insight it would be very much appreciated!

    Thanks,
    -Adam

  • #2
    Re: Script to Change PC Name to Serial Number

    I just noticed that there is a scripting forum. I apologize If I have posted this in the wrong place.

    Comment


    • #3
      Re: Script to Change PC Name to Serial Number

      Moving to the scripting forum for better response.

      Comment


      • #4
        Re: Script to Change PC Name to Serial Number

        Wouldn't it be easier to start over?
        Just leave the current infrastructure (don't fix a running system) and start building a AD environment, create a WDS image or so and then re-image one by one?
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          Re: Script to Change PC Name to Serial Number

          In a perfect world... yes.

          Don't worry, I have plenty of rage built up about the mismanagement here, but I'll leave that for a later date.

          Your idea is great, BUT not only do I not have the manpower at the moment, but I also haven't started investigating AD solutions.

          There is much policy to make and much cleanup to do. Renaming at least the new computers going out to users would have a good effect down the road for expansion and migration.

          Comment


          • #6
            Re: Script to Change PC Name to Serial Number

            I actually did get this script, but was recommended not to use it since it edits the registry.

            Code:
            strComputer = "."
            Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
            Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
            For each objBIOS in colBIOS
            StrNewPCName=objBIOS.SerialNumber
            Next
            Set MyShell= CreateObject ("WSCript.shell")
            StrCurrentControlSet = "HKLM\SYSTEM\CurrentControlSet\"
            StrTcpipParams = StrCurrentControlSet & "services\Tcpip\Parameters\"
            StrComputerName = StrCurrentControlSet & "Control\ComputerName\"
            With MyShell
            .RegDelete StrTcpipParams & "Hostname"
            .RegDelete StrTcpipParams & "NV Hostname"
            .RegWrite StrComputerName & "ComputerName\ComputerName", StrNewPCName
            .RegWrite StrComputerName & "ActiveComputerName\ComputerName", StrNewPCName
            .RegWrite StrTcpipParams & "Hostname", StrNewPCName
            .RegWrite StrTcpipParams & "NV Hostname", StrNewPCName
            End With
            The person who gave me that code recommended that I use the SetComputerNameEx function, which I believe is C++ and can be found here.

            Sadly, I am not a C++ programmer, what do you recommend?

            Thanks again,
            -AW

            Comment


            • #7
              Re: Script to Change PC Name to Serial Number

              Originally posted by somedudeppf View Post
              I am not a C++ programmer, what do you recommend?
              How about using WMI via some VBScript? This example is from MSDN:

              Code:
              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
              Here is another one:

              Code:
              strComputer = "."
              Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" _
                   & strComputer & "\root\cimv2")
              Set colComputers = objWMIService.ExecQuery _
                  ("Select * from Win32_ComputerSystem")
              For Each objComputer in colComputers
                  errReturn = ObjComputer.Rename("NewName")
                  WScript.Echo "Computer name is now " & objComputer.Name
              
              Next
              All you need to do is add picking the serial number, and you already got that one with Win32_BIOS WMI call.
              -vP

              Comment

              Working...
              X