MSFN Forum: Need help scripting computer rename (non-domain) - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need help scripting computer rename (non-domain) Imaging multiple PCs, and need to rename each one Rate Topic: -----

#1 User is offline   Nathan Plant 

  • Group: Members
  • Posts: 5
  • Joined: 10-January 08

  Posted 10 January 2008 - 03:34 PM

I'm very new to this, so please bear with me as I'm learning the terminology.

The goal:
To use Sysprep with an answer file to restore an image to a new PC, without having to baby-sit the process. This would include renaming the computer based on the "SerialNumber" field in the BIOS.

The problem:
I've got a simple VB script already that will pull the SerialNumber from the BIOS, but now I can't figure out how to use the information to rename the system. Specifically, the computer name will need to be "CompanyName-SerialNumber". Is there an easy way to perform the rename within the same script that I use to pull the SerialNumber out of the BIOS?

Note: I don't need to join these computers to a domain - this is for preparing multiple systems for a customer who will use them stand-alone.

My existing script that pulls the SerialNumber is below. Right now, I have it set to port the output to a text file, and one of our developers wrote a little program to parse the file and rename the computer, but this is not as elegant as I think it could be. Since I don't know how to write script yet, I pieced this together from Scriptomatic and some samples from the Web.

Script:

filePath = "C:\"
filePrefix = "ID"
fileExt = ".txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(filePath & filePrefix & fileExt)

arrComputers = Array(".")
For Each strComputer In arrComputers

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", _
										  wbemFlagReturnImmediately + wbemFlagForwardOnly)
  
   For Each objItem In colItems
	 objFile.WriteLine objItem.SerialNumber
   Next
Next


Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm: 
	WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
	Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
	& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function



Thanks in advance for any help,
Nathan


#2 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,249
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 10 January 2008 - 08:30 PM

How about this, it will pull the serial number from the machine using your code, more or less, then rename the machine to the serial number:

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", _
	wbemFlagReturnImmediately + wbemFlagForwardOnly)
  
For Each objItem In colItems
	newComputerName = objItem.SerialNumber
Next

Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
	MsgBox "About to rename computer to: " & newComputername
	ErrCode = objComputer.Rename(newComputerName)
	If ErrCode = 0 Then
		MsgBox "Computer renamed correctly."
	Else
		MsgBox "Changing computer name failed. Error code: " & ErrCode
	End If
Next


To rename it to 'Company Name' + serial number change:
newComputerName = objItem.SerialNumber
to
newComputerName = "CompanyName" & objItem.SerialNumber

The lines which contain MSGBOX are for notification only and can be removed if so desired, though I would recommend leaving the failed message so you know without having to look if it didn't work.

#3 User is offline   Nathan Plant 

  • Group: Members
  • Posts: 5
  • Joined: 10-January 08

Posted 11 January 2008 - 01:43 PM

IcemanND -

That worked well. And much more elegant than my newbie code, I must say. I stripped out the message boxes so that the user doesn't have to interface unless there's an error, and added the CompanyName string like you mentioned, and it's a hit.

Thanks again for the help! Now... to get a book or two to get more into it.

-Nathan

#4 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,249
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 January 2008 - 01:52 PM

Check out https://www.microsof...et/scriptcenter for some good examples and the 'Scripting Guys' columns with detail answers to user questions.

#5 User is offline   Nathan Plant 

  • Group: Members
  • Posts: 5
  • Joined: 10-January 08

Posted 30 January 2008 - 04:59 PM

I've been reading up on this stuff, and finally getting the time to incorporate the script into my sysprep routine, but I'm running into another problem now, and I'm lost as to the trouble.

I'm including the script above (minus the user-interactive parts, since it's supposed to be largely unattended) in the cmdlines.txt for Sysprep use (called by a batch file that uses "cscript <scriptname.vbs>" as it's only line). When Sysprep executes the script, I get the following error.

"C:\Sysprep\i386\$oem$>rencomp.vbs (17,1) SWbemServicesEx: Critical Error"

I've searched and tried to understand what is happening here, and I figure it has to do with the Mini Setup I'm running, but I don't know enough about the environment in which this script is running to know for sure if there's a problem there or not. The script runs perfectly when logged in as an administrator, by the way.

My easy solution is to include the batch file that runs the script in the "run_once_GUI" section of the Sysprep ini, but then I'll have to leave something behind (like a batch file that doesn't go anywhere). I'd rather have things clean themselves up, but I'll live with it if it's all I can get.

Thanks for the referral to the Scripting Guys, too. I've been reading a lot of stuff there.

-Nathan

#6 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,249
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 30 January 2008 - 07:05 PM

It is probably running before a service is available for the script to run. It might work better to run the registry version of the script that just rights the new name to the registry. (reboot required for it to take full effect)

Set shell = CreateObject("WScript.Shell")
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", NewName
Shell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", NewName


#7 User is offline   Nathan Plant 

  • Group: Members
  • Posts: 5
  • Joined: 10-January 08

Posted 31 January 2008 - 01:11 PM

Thanks again. I'll give it a try that way, and see what happens. I'm rebooting after the mini-setup anyway, so it should be fine.

#8 User is offline   Nathan Plant 

  • Group: Members
  • Posts: 5
  • Joined: 10-January 08

Posted 01 February 2008 - 11:26 AM

Hmmm.. well, it still fails at the same line, which is where the WIN32_Bios class is first called. So I'm thinking that I'll go to my backup method for the project at hand (folks want this done soon), and then try to learn more about the environment and how to make it more efficient later.

Thanks again, IcemanND for the help. Hopefully one day I can return the favor.

-Nathan

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy