Solved

Rename computer with VBscript and prepending with static name

Posted on 2010-09-23
16
1,143 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

14 Experts available now in Live!

Get 1:1 Help Now