Solved

Rename computer with VBscript and prepending with static name

Posted on 2010-09-23
16
1,134 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now