Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Rename computer with VBscript and prepending with static name

Posted on 2010-09-23
16
1,176 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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