Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rename computer with VBscript and prepending with static name

Posted on 2010-09-23
16
Medium Priority
?
1,285 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 29

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 29

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 29

Expert Comment

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

Option Explicit

0
 
LVL 29

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 29

Assisted Solution

by:sammySeltzer
sammySeltzer earned 2000 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 29

Expert Comment

by:sammySeltzer
ID: 33758320
hmm,

I tested without error.

Hold...
0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 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 29

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

877 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