Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Renaming computer based on Serial number AND chassis Type

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Covert Ops

Covert Ops
  • Member
  • 3 posts
  • Joined 17-June 08
Hello

I am trying to rename a bunch of workstations based on their serial number, and then a "D-" at the beginning if it is a workstation, and a "M-" if it is a notebook/laptop

For example a workstation would be renamed to D-SN12345 and a laptop would be renamed to M-SN12345

So far I have this:
Set objNetwork = CreateObject("WScript.Network")
	strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
		strComputer & "'")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
	strMsg = objItem.Caption
	If strMsg = "Microsoft Windows XP Professional" Then

		winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//."
		'WScript.Echo winmgmt1
			Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
			for each SN in SNSet

				If SN.SerialNumber = strComputer Then
					MsgBox "Computer name already set! Name is: "& strComputer
					Wscript.Quit
				End If

				If SN.SerialNumber = "" Then
					MsgBox "Problem with computer naming!"
					Wscript.Quit
				End If

				ErrCode = objComputer.Rename(SN.SerialNumber, strPassword, strUser)

				If ErrCode = 0 Then
					MsgBox "Computer renamed successfully to: "& SN.SerialNumber
				End If

			Next

	End If
Next

This works well, but I am not sure how to go about determining if the computer is a workstation or a notebook, and then how to go about adding it to the beginning of the computer name

I did not write the script above, and I have very limited scripting knowledge

Any help would be GREATLY appreciated


How to remove advertisement from MSFN

#2
Mijzelf

Mijzelf

    Advanced Member

  • Member
  • PipPipPip
  • 462 posts
  • Joined 11-April 07

I am not sure how to go about determining if the computer is a workstation or a notebook

Maybe you can check the battery status?

#3
No6

No6

    Member

  • Member
  • PipPip
  • 108 posts
  • Joined 25-February 05
The following may be of help:

http://www.microsoft...04/hey0921.mspx
http://www.novell.co...ture/15533.html

#4
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,421 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Here is a script that based on the link from Microsoft, I have it so it show type and takes the first letter.

Option Explicit
Dim ColItems, ComType, ObjItem, strType, Wmi

Set Wmi= GetObject("winmgmts:\\.\root\CIMV2") 
Set ColItems = Wmi.ExecQuery("SELECT * FROM Win32_SystemEnclosure",,48)

 For Each ObjItem in ColItems
  For  Each strType in ObjItem.ChassisTypes
  Select Case strType
   Case 1 ComType = "Other"
   Case 2 ComType = "Unknown"
   Case 3 ComType = "Desktop"
   Case 4 ComType = "Low Profile Desktop"
   Case 5 ComType = "Pizza Box"
   Case 6 ComType = "Mini Tower"
   Case 7 ComType = "Tower"
   Case 8 ComType = "Portable"
   Case 9 ComType = "Laptop"
   Case 10 ComType = "Notebook"
   Case 11 ComType = "Handheld"
   Case 12 ComType = "Docking Station"
   Case 13 ComType = "All-in-One"
   Case 14 ComType = "Sub-Notebook"
   Case 15 ComType = "Space Saving"
   Case 16 ComType = "Lunch Box"
   Case 17 ComType = "Main System Chassis"
   Case 18 ComType = "Expansion Chassis"
   Case 19 ComType = "Sub-Chassis"
   Case 20 ComType = "Bus Expansion Chassis"
   Case 21 ComType = "Peripheral Chassis"
   Case 22 ComType = "Storage Chassis"
   Case 23 ComType = "Rack Mount Chassis"
   Case 24 ComType = "Sealed-Case PC"
   Case Else ComType = "Unknown"
  End Select
 Next
 Next  
 WScript.Echo ComType & vbTab & Left(ComType,1)




GunSmokingMan



#5
Covert Ops

Covert Ops
  • Member
  • 3 posts
  • Joined 17-June 08
Great! Thanks for the help guys.

I think this will work well:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
	("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
	For  Each strChassisType in objChassis.ChassisTypes
		Wscript.Echo strChassisType
	Next
Next

But I am not sure how to incorporate it into my previous script, and then have it enter a D- or M- at the beginning of the computer name

Edited by Covert Ops, 18 June 2008 - 08:26 AM.


#6
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,421 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Try this script it should do what you want it to do.

Option Explicit
 Dim ColItems, ComType, ErrCode, Net, ObjItem, ObjReName, ObjSN, SerialNum
 Dim StrComputer, StrPassword, StrType, StrUser, Wmi, WmiVar, Var1
 
  Set Net = CreateObject("WScript.Network")
	  StrComputer = Net.ComputerName
  Set Wmi= GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2")
'/-> Start The Script
  ComputerType()
'/-> Get Pc Type
   Function ComputerType()
	Set ColItems = Wmi.ExecQuery("SELECT * FROM Win32_SystemEnclosure",,48)
	 For Each ObjItem in ColItems
	  For Each StrType in ObjItem.ChassisTypes
	   Select Case StrType
		Case 1 ComType = "Other"
		Case 2 ComType = "Unknown"
		Case 3 ComType = "Desktop"
		Case 4 ComType = "Low Profile Desktop"
		Case 5 ComType = "Pizza Box"
		Case 6 ComType = "Mini Tower"
		Case 7 ComType = "Tower"
		Case 8 ComType = "Portable"
		Case 9 ComType = "Laptop"
		Case 10 ComType = "Notebook"
		Case 11 ComType = "Handheld"
		Case 12 ComType = "Docking Station"
		Case 13 ComType = "All-in-One"
		Case 14 ComType = "Sub-Notebook"
		Case 15 ComType = "Space Saving"
		Case 16 ComType = "Lunch Box"
		Case 17 ComType = "Main System Chassis"
		Case 18 ComType = "Expansion Chassis"
		Case 19 ComType = "Sub-Chassis"
		Case 20 ComType = "Bus Expansion Chassis"
		Case 21 ComType = "Peripheral Chassis"
		Case 22 ComType = "Storage Chassis"
		Case 23 ComType = "Rack Mount Chassis"
		Case 24 ComType = "Sealed-Case PC"
		Case Else ComType = "Unknown"
	  End Select
	 Next
	Next
'/-> Get The Computer Serial Number
	ComputerSerialNumber()
   End Function
'/-> Get Serial Number
   Function ComputerSerialNumber()
	Set ColItems = Wmi.ExecQuery("Select * from Win32_OperatingSystem",,48)
	For Each ObjItem In ColItems
	 If InStr(LCase(ObjItem.Caption),LCase("XP Professional")) Then
	 WmiVar = "winmgmts:{impersonationLevel=impersonate}!//."
	 Set SerialNum = GetObject(WmiVar).InstancesOf ("Win32_BIOS")
	  For  Each ObjSN In SerialNum
	   If ObjSN.SerialNumber = strComputer Then
		 MsgBox "This Computer Name already taken!" & vbCrLf &_
		 "Computer Name: "& StrComputer,4128, "Computer Name In Use"
		 Wscript.Quit
	   End If
	   If InStr(LCase(ObjSN.SerialNumber),LCase("System")) Then
		MsgBox "Error Cannot Retrieve This Computer Serial Number!",4128,"Error No Serial Number"
		Wscript.Quit
	   End If
	   If SN.SerialNumber = "" Then
		MsgBox "Error Cannot Retrieve This Computer Serial Number!",4128,"Error No Serial Number"
		Wscript.Quit
	   End If
	   If Len(ObjSN.SerialNumber) => 1 Then
	   Var1 = Left(ComType,1) & "-" & ObjSN.SerialNumber
		Set ObjReName = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
		strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" &  strComputer & "'")
		ObjReName.Rename(Var1, StrPassword, StrUser)
		If ErrCode = 0 Then 
		 MsgBox "The Computer Rename Has Been Completed" & vbCrLf & _
		 "New Name :" & Var1, 4128,"Rename Completed"
		End If 
	   End If 
	  Next 
	 End If
	Next
   End Function




GunSmokingMan



#7
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Try this script it should do what you want it to do.

A few issues...

whatever.vbs(72, 53) Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub

The fix:
ObjReName.Rename Var1, StrPassword, StrUser

Next...
If ObjSN.SerialNumber = strComputer Then
You're testing if the computer is named by its serial number alone, which is not the format you're actually trying to rename to (might be a total non-issue, as long as you didn't go around naming your PCs by serial # alone first)

Next...
If SN.SerialNumber = "" Then

Microsoft VBScript runtime error: Variable is undefined: 'SN'

fix:
If ObjSN.SerialNumber = "" Then

Next...
If ObjSN.SerialNumber = "" Then
and
If Len(ObjSN.SerialNumber) => 1 Then
failed to take in account that a lot of PCs w/o a serial number return a 1 character string, consisting of a single space (0x20, 32 dec), this would force the most part of our desktops to be renamed as "D- " and laptops as "L- " (thankfully it doesn't work, due to the trailing space). StrPassword and StrUser are empty too (not null but empty), not sure if that would even work, depends what it transforms the empty values into before making the API call -- a 0 length string or a null (haven't tried, too lazy!)

Next...
If ErrCode = 0 Then
ErrCode is empty, so it falsely says it has been renamed, even when it fails. (try running: If varthatdoesntexist = 0 Then MsgBox "nice!" -- nice eh?) Perhaps you meant to use Err, but that won't catch errors of WMI classes' methods, you need to do something like:
intResult = ObjReName.Rename (Var1, StrPassword, StrUser)
(Yes, NOW parentheses are required, and don't forget intResult needs to be dim'ed since you're using option explicit)
Here, intResult returns 87 indicating it actually failed.

Also, the rename method of the Win32_ComputerSystem class has limited OS support (hence why you made your script for XP only perhaps), and it will break domain memberships too (not fun) because it has to be renamed in there too! (unless it's smart enough to throw an error message and fail instead), making the script unusable for most businesses. For this reason alone, it's better to use the wsname utility, which handles all that stuff (i.e. domain memberships), and will accomplish this task (assuming your serial #s aren't blank), without a script at all i.e. using the /N:$CHASSIS[0]-$SERIALNUM argument, free of bugs too.

And that's ignoring that a large portion of computers (unless you only buy Dell) don't have the serial # filled in their BIOS in the first place, making this age-old problem not quite that easily solvable. More often than not, it's done from a list of mac addresses <-> computer name table, mainly because that never fails. (you just have to retrieve the mac addresses of your PCs first, along with their current names, with yet another script)

Also, not totally sure why you're using functions in a linear program flow (main calls ComputerType right under it, which then calls ComputerSerialNumber right under it, then returns from ComputerSerialNumber to ComputerType, which immediately returns to main...). And them functions are only called once (might be a reused snippet, that would explain it)

Edit: typo

Edited by crahak, 18 June 2008 - 10:22 PM.

Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#8
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,421 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag

Also, not totally sure why you're using functions in a linear program flow (main calls ComputerType right under it, which then calls ComputerSerialNumber right under it, then returns from ComputerSerialNumber to ComputerType, which immediately returns to main...). And them functions are only called once (might be a reused snippet, that would explain it)


The function are there just to make it easier for him to edit. I was not sure how he was going to get StrPassword, StrUser, I was going to add another function to get that info.

Since I do not know what he wants to do I just wrote him a script with what was provided, I only tested to make sure it runs without errors, I had the rename part comment so that how I missed that.

Here is a updated version with the corrections. I changed the rename part, with this link
Rename Method of the Win32_ComputerSystem Class

Option Explicit
 Dim ColItems, ComType, ErrCode, Net, ObjItem, ObjReName, ObjSN, Return, SerialNum
 Dim StrComputer, StrPassword, StrType, StrUser, Wmi, WmiVar, Var1
 
  Set Net = CreateObject("WScript.Network")
	  StrComputer = Net.ComputerName
  Set Wmi= GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2")
'/-> Start The Script
  ComputerType()
'/-> Get Pc Type
   Function ComputerType()
	Set ColItems = Wmi.ExecQuery("SELECT * FROM Win32_SystemEnclosure",,48)
	 For Each ObjItem in ColItems
	  For Each StrType in ObjItem.ChassisTypes
	   Select Case StrType
		Case 1 ComType = "Other"
		Case 2 ComType = "Unknown"
		Case 3 ComType = "Desktop"
		Case 4 ComType = "Low Profile Desktop"
		Case 5 ComType = "Pizza Box"
		Case 6 ComType = "Mini Tower"
		Case 7 ComType = "Tower"
		Case 8 ComType = "Portable"
		Case 9 ComType = "Laptop"
		Case 10 ComType = "Notebook"
		Case 11 ComType = "Handheld"
		Case 12 ComType = "Docking Station"
		Case 13 ComType = "All-in-One"
		Case 14 ComType = "Sub-Notebook"
		Case 15 ComType = "Space Saving"
		Case 16 ComType = "Lunch Box"
		Case 17 ComType = "Main System Chassis"
		Case 18 ComType = "Expansion Chassis"
		Case 19 ComType = "Sub-Chassis"
		Case 20 ComType = "Bus Expansion Chassis"
		Case 21 ComType = "Peripheral Chassis"
		Case 22 ComType = "Storage Chassis"
		Case 23 ComType = "Rack Mount Chassis"
		Case 24 ComType = "Sealed-Case PC"
		Case Else ComType = "Unknown"
	  End Select
	 Next
	Next
'/-> Get The Computer Serial Number
	ComputerSerialNumber()
   End Function
'/-> Get Serial Number
   Function ComputerSerialNumber()
	Set ColItems = Wmi.ExecQuery("Select * from Win32_OperatingSystem",,48)
	For Each ObjItem In ColItems
	 If InStr(LCase(ObjItem.Caption),LCase("XP Professional")) Then
	 WmiVar = "winmgmts:{impersonationLevel=impersonate}!//."
	 Set SerialNum = GetObject(WmiVar).InstancesOf ("Win32_BIOS")
	  For  Each ObjSN In SerialNum
	   If ObjSN.SerialNumber = strComputer Then
		 MsgBox "This Computer Name already taken!" & vbCrLf &_
		 "Computer Name: "& StrComputer,4128, "Computer Name In Use"
		 Wscript.Quit
	   End If
	   If InStr(LCase(ObjSN.SerialNumber),LCase("System")) Then
		MsgBox "Error 1 Cannot Retrieve This Computer Serial Number!",4128,"Error 1 System Serial Number"
		Wscript.Quit
	   End If
	   If ObjSN.SerialNumber = "" Then
		MsgBox "Error 2 Cannot Retrieve This Computer Serial Number!",4128,"Error 2 No Serial Number"
		Wscript.Quit
	   End If
	   If Len(ObjSN.SerialNumber) => 2 Then
	   Var1 = Left(ComType,1) & "-" & ObjSN.SerialNumber
		For Each ObjReName In Wmi.InstancesOf("Win32_ComputerSystem")
		 Return = ObjReName.Rename(Var1, StrPassword, StrUser)
		 If Return <> 0 Then 
		  MsgBox "Error 3 The Renaming Of This Computer Has Failed Error = " &_
		   Err.Number,4128,"Error 3 Rename Failed"
		 Else 
		  MsgBox "The Computer Rename Has Been Completed" & vbCrLf & _
		 "New Name :" & Var1 & vbCrLf & "Reboot The Computer To Apply The New Name", 4128,"Rename Completed"
		 End If 
		Next 
	   End If 
	  Next 
	 End If
	Next
   End Function




GunSmokingMan



#9
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag
No more typos :)

But there still remain some other issues.

One thing I just thought of, is he wants computers named D-serial or M-serial

Your script will likely rename some laptops to P-serial (Portable), L-serial (Laptop), N-serial (Notebook), S-serial (Sub-Notebook), D-serial (if on a docking station) and perhaps even H-serial (Handheld), depending on the value of the ChassisType property. Similarly desktops could be named L-serial (Low Profile Desktop), M-serial (Mini Tower) which is what he wanted to name laptops(!), T-serial (Tower), etc.

And still, the script remains XP-only, and only works if your computers are part of a workgroup (doesn't work with domains) which is a serious limitation. And there's nothing you can do about the countless computers without a serial number in the DMI tables of the SMBIOS.

wsname will let you rename without any script at all, as intended, with he /N:$CHASSIS[0]-$SERIALNUM argument. It's been time tested by a LOT of people, bugfixed and all. And it works on *every* version of windows (i.e. 9x, NT, and the still fairly popular win2k). And it works even if your computers are part of a domain (most enterprise computers would be). It also has plenty of other handy functions like logging, it integrates nicely with disk imaging/sysprep solutions, can set other infos, etc. Should you decide to rename based on mac addresses (when you find out more than half your computers don't have a serial number), then it can do that too, using these arguments /RDF:macaddytoname.txt /DFK:$MAC and a properly formatted simple text file (macaddyhere = correspondingname). It even has a mechanism to ignore certain types of network adapters for that purpose.

I like scripting as much as the next guy, but when there's a specialized utility that already does this in a better way, why bother?
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#10
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,421 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag

One thing I just thought of, is he wants computers named D-serial or M-serial


I am just trying to provide a template for Covert Ops, it would be up to him to edit it to what he wanted.


GunSmokingMan



#11
Covert Ops

Covert Ops
  • Member
  • 3 posts
  • Joined 17-June 08

One thing I just thought of, is he wants computers named D-serial or M-serial


I am just trying to provide a template for Covert Ops, it would be up to him to edit it to what he wanted.



This worked for me. I modified it to just output D- or M-

Thank you all for the help!!

#12
mobious99

mobious99

    Newbie

  • Member
  • 25 posts
  • Joined 28-May 08
here's the script I cobbed together

asks for computer name
- checks for the length to make sure no longer than 13 (netbios names freak out around 15)
- checks the length for 0 characters
- checks the name for bogus characters

strcn = InputBox("Change Name to the following","Please Enter Computername", compname)
Name = strcn
name= UCase(Name)
If Len(name) > 13 Then
Wscript.echo "The name is too long for use with wins"
wscript.quit
End If

If Len(name) < 1 Then
Wscript.echo "The name is too short to be used"
wscript.quit
End If

Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True
objRegEx.Pattern = "[^A-Z0-9]"

strSearchString = name

Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then
Wscript.Echo "The following characters are not allowed:"
For Each strMatch in colMatches
Wscript.Echo strMatch.Value
wscript.Quit
Next
End If

strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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

#13
Yens

Yens
  • Member
  • 2 posts
  • Joined 15-March 12
  • OS:Windows 7 x86
  • Country: Country Flag

here's the script I cobbed together

asks for computer name
- checks for the length to make sure no longer than 13 (netbios names freak out around 15)
- checks the length for 0 characters
- checks the name for bogus characters

strcn = InputBox("Change Name to the following","Please Enter Computername", compname)
Name = strcn
name= UCase(Name)
If Len(name) > 13 Then
Wscript.echo "The name is too long for use with wins"
wscript.quit
End If

If Len(name) < 1 Then
Wscript.echo "The name is too short to be used"
wscript.quit
End If

Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True
objRegEx.Pattern = "[^A-Z0-9]"

strSearchString = name

Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then
Wscript.Echo "The following characters are not allowed:"
For Each strMatch in colMatches
Wscript.Echo strMatch.Value
wscript.Quit
Next
End If

strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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


This doesn't work

#14
mobious99

mobious99

    Newbie

  • Member
  • 25 posts
  • Joined 28-May 08

This doesn't work


updated.

strcn = InputBox("Change Name to the following","Please Enter Computername", compname)
Name = strcn
name= UCase(Name)
If Len(name) > 15 Then
    Wscript.echo "The name is too long for use with wins"
    wscript.quit
End If
 
If Len(name) < 1 Then
    Wscript.echo "The name is too short to be used"
    wscript.quit
End If
 
Set objRegEx = CreateObject("VBScript.RegExp")
 
objRegEx.Global = True   
objRegEx.Pattern = "[^A-Z0-9]"
 
strSearchString = name
 
Set colMatches = objRegEx.Execute(strSearchString)  
 
If colMatches.Count > 0 Then
    Wscript.Echo "The following characters are not allowed:"
    For Each strMatch in colMatches   
        Wscript.Echo  strMatch.Value
                wscript.Quit
    Next
End If
 
strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users