Solved

Rename computer with VBscript and prepending with static name

Posted on 2010-09-23
16
1,159 Views
Last Modified: 2012-05-10
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


 
0
Comment
Question by:adamshields
  • 8
  • 8
16 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33748956
0
 
LVL 3

Author Comment

by:adamshields
ID: 33749197
I was able to change
Name = strSN
to
Name = "ABC123" & strSN
0
 
LVL 3

Author Comment

by:adamshields
ID: 33756809
Actually I tested this on Windows XP and it worked but it is not working on Windows 7. Anyone know why?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 3

Author Comment

by:adamshields
ID: 33756811
Actually I tested this on Windows XP and it worked but it is not working on Windows 7. Anyone know why?
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33756981
when you said, "it is not working", what exactly do you mean?

What error are you getting?
0
 
LVL 3

Author Comment

by:adamshields
ID: 33757050
"There was an error renaming the machine. Please restart, and try again."

Is there way to output a more specific error?
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33757143
Ok, add this at top of page and run the script again:

Option Explicit

0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33757174
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.
0
 
LVL 3

Author Comment

by:adamshields
ID: 33757613
Do you know how to fix that?
0
 
LVL 28

Assisted Solution

by:sammySeltzer
sammySeltzer earned 500 total points
ID: 33757898
just Dim it

Dim strComputer

That's it.

If you have more, then delimit each with a comma like:

Dim strComputer,objWMIService,colSMBIOS,objSMBIOS,strSN,Name,ColComputers,objComputer

You can just copy this to line just above:

strComputer = "."
0
 
LVL 3

Author Comment

by:adamshields
ID: 33758053
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......
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33758320
hmm,

I tested without error.

Hold...
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 33758364
the code seems fine.

Please ensure that you have admin rights to your pc.

go to user accounts and check your permission level.

If it isn't admin level, change it so.

if it is, pls let me know
0
 
LVL 3

Author Comment

by:adamshields
ID: 33758955
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

0
 
LVL 3

Author Closing Comment

by:adamshields
ID: 33758961
Thanks
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33759183
I am happy for you and thanks for the points and grade.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question