Dynamically Renaming a Computer with VBScript

Helped out a colleague recently with an interesting scenario; as part of an Active Directory consolidation project and reimage strategy, he wanted to have a computer automatically renamed then rebooted. Computer names should be unique within each site. PowerShell was not an option for us, since one, the script ran locally on each computer during login process and PowerShell was not installed on these devices.

Further, these actions should be part of a login script. 

General specifications were given as follows:

Computer name should be as follows: Site ID – Des ID + Device ID + Last (7) Digits of Serial Number

Site ID: XXX

Destination ID: Y

Device ID: L = laptop, D = Desktop

So the name should look like this: 123-SL1234567

Begin VBScript here…

Option Explicit

Dim Text, Title

Dim WshNetwork         ' Object variable

Dim strComputer, strChassis

Dim objWMIService, objChassis, colChassis, objItem

dim objComputer, colComputers, intErrorCode, objOutputFile

Dim DeviceID, SiteID, DesID

Dim strNewName, strComputerName, strResp

Dim objSWbemLocator, objWMIService2, colBIOS, serial

Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")

' Create a new WshNetwork object to access network properties.

Set WshNetwork = WScript.CreateObject("WScript.Network")

'**** CHANGE NEXT TWO ENTRIES AS NEEDED

SiteID = "XXX"

DesID = "Y"

strComputer = "."

'Get Chassis type from WMI

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colChassis = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure",,16)

'Read the Chassis type number and set accordindly

For Each objChassis in colChassis

  For Each objItem in objChassis.ChassisTypes

    Select Case objItem

                    Case 1 strChassis          = "Virtual Machine"                                       ' "Other"

                    Case 2 strChassis           = "Unknown"                                                   ' "Unknown"

                    Case 3 strChassis           = "Desktop"                                                      ' "Desktop"

                    Case 4 strChassis           = "Desktop"                                                      ' "Low Profile Desktop"

                    Case 5 strChassis           = "Server"                                                          ' "Pizza Box "

                    Case 6 strChassis           = "Desktop"                                                      ' "Mini Tower"

                    Case 7 strChassis           = "Desktop"                                                      ' "Tower"

                    Case 8 strChassis          = "Laptop"                                                         ' "Portable"

                    Case 9 strChassis           = "Laptop"                                                         ' "Laptop"

                    Case 10 strChassis         = "Laptop"                                                         ' "Notebook"

                    Case 11 strChassis         = "Laptop"                                                         ' "Hand Held"

                    Case 12 strChassis         = "Laptop"                                                         ' "Docking Station"

                    Case 13 strChassis         = "Desktop"                                                      ' "All in One"

                    Case 14 strChassis         = "Laptop"                                                         ' "Sub Notebook"

                    Case 15 strChassis         = "Desktop"                                                      ' "Space-Saving"

                    Case 16 strChassis         = "Desktop"                                                      ' "Lunch Box "

                    Case 17 strChassis         = "Server"                                                          ' "Main System Chassis"

                    Case 18 strChassis         = "Desktop"                                                      ' "Expansion Chassis"

                    Case 19 strChassis         = "Server"                                                          ' "SubChassis"

                    Case 20 strChassis         = "Server"                                                          ' "Bus Expansion Chassis"

                    Case 21 strChassis         = "Server"                                                          ' "Peripheral Chassis"

                    Case 22 strChassis         = "Server"                                                          ' "Storage Chassis"

                    Case 23 strChassis         = "Server"                                                          ' "Rack Mount Chassis"

                    Case 24 strChassis   = "Desktop"                                                            ' "Sealed-Case PC"

                                Case Else strChassis = "Unknown"

    End Select

  Next

Next

If strChassis = "Laptop" Then

                DeviceID = "L"

Else

                DeviceID = "D"

End If

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set objWMIService2 = objSWbemLocator.ConnectServer(strComputer, "root\cimv2")

Set colBIOS = objWMIService2.ExecQuery("SELECT * FROM Win32_BIOS")

'Get serial number here

For Each objItem in colBIOS

                serial = RTrim(objItem.SerialNumber)

Next

'Trim serial number to get only the last 7 digits

serial = Right(serial,7)

strNewName = SiteID & "-" & DesID & DeviceID & serial

' Remark the following line to suppress display

' wscript.echo strNewName

'****

strComputerName = strNewName

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")

For Each objComputer in colComputers

'    wscript.echo "objComputer.Name: " & objComputer.Name

'    wscript.echo "strNewName: " & strNewName

    If objComputer.Name <> strNewName Then

       ' rename this computer

       intErrorCode = objComputer.Rename(strComputerName)

       ' now reboot

       Reboot

    End if

Next

Sub Reboot()

dim strComputer, objWMIService, colOS, objOS

  strComputer = "." ' Local Computer

  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\" & _

                                                strComputer & "\root\cimv2")

  Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

  For Each objOS in colOS

                objOS.Reboot()

  Next

End Sub

End VBScript here…

Published Friday, November 20, 2009 6:18 PM by sthompson
Filed under: ,

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems