Link to home
Start Free TrialLog in
Avatar of adamshields
adamshieldsFlag for United States of America

asked on

Rename computer with VBscript and prepending with static name

The following code successfully renames a Dell host based on it's service tag. I need to prefix the new computer name with the site name so for example I would like to prefix the service tag with ABC123 so the new name would be ABC123SERVTAG.

'***********************************************************************
'
' changecomputername-Win7.vbs
' Changes the computer name to the computer's serial number/service tag.
' A message is displayed if an error occurs, or if the result is success.
'
' Joe Grossi - June 11, 2010
' FMCNA
'
'***********************************************************************

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

'Part 1 - Acquire serial/service tag number
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
  strSN = objSMBIOS.SerialNumber
  If strSN <> "" Then exit For
Next 

'Part 2 - Assign computer name to serial/service tag number
Name = strSN

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

For Each objComputer in colComputers
    err = objComputer.Rename(name)

if err <> 0 then
 wscript.echo "There was an error renaming the machine. Please restart, and try again."
else
 wscript.echo "Machine successfully renamed: " & Name
end if

Next

Open in new window


 
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Avatar of adamshields

ASKER

I was able to change
Name = strSN
to
Name = "ABC123" & strSN
Actually I tested this on Windows XP and it worked but it is not working on Windows 7. Anyone know why?
Actually I tested this on Windows XP and it worked but it is not working on Windows 7. Anyone know why?
when you said, "it is not working", what exactly do you mean?

What error are you getting?
"There was an error renaming the machine. Please restart, and try again."

Is there way to output a more specific error?
Ok, add this at top of page and run the script again:

Option Explicit

You will see the problem once you do that.

strComputer is undefined.

There maybe more but option Explicit usually leads you to the culprit.

Good idea to include in your files.
Do you know how to fix that?
SOLUTION
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I put in the Dim strComputer,objWMIService,colSMBIOS,objSMBIOS,strSN,Name,ColComputers,objComputer but now I don't get a specific error it just says There was an error renaming the machi......
hmm,

I tested without error.

Hold...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, had admin permissions but UAC is on so I found a tidbit of code to make it work correctly.
'***********************************************************************
'
' changecomputername-Win7.vbs
' Changes the computer name to the computer's serial number/service tag.
' A message is displayed if an error occurs, or if the result is success.
'
' Joe Grossi - June 11, 2010
' FMCNA
'
'***********************************************************************


Option Explicit
Dim strComputer,objWMIService,colSMBIOS,objSMBIOS,strSN,Name,ColComputers,objComputer,objShell

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

'Part 1 - Acquire serial/service tag number
Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
  strSN = objSMBIOS.SerialNumber
  If strSN <> "" Then exit For
Next 

'Check for UAC
If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")
  'Pass a bogus argument with leading blank space, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

'Part 2 - Assign computer name to serial/service tag number
Name = strSN

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

For Each objComputer in colComputers
    err = objComputer.Rename(Name)

if err <> 0 then
 wscript.echo "There was an error renaming the machine. Please restart, and try again."
else
 wscript.echo "Machine successfully renamed: " & Name & " Please reboot!"
end if

Next

'End the UAC check
End If

Open in new window

Thanks
I am happy for you and thanks for the points and grade.