Nathan Plant Posted January 10, 2008 Posted January 10, 2008 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 NextNextFunction 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 FunctionThanks in advance for any help,Nathan
IcemanND Posted January 11, 2008 Posted January 11, 2008 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.SerialNumberNextSet 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 IfNextTo rename it to 'Company Name' + serial number change: newComputerName = objItem.SerialNumberto newComputerName = "CompanyName" & objItem.SerialNumberThe 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.
Nathan Plant Posted January 11, 2008 Author Posted January 11, 2008 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
IcemanND Posted January 11, 2008 Posted January 11, 2008 Check out https://www.microsoft.com/technet/scriptcenter for some good examples and the 'Scripting Guys' columns with detail answers to user questions.
Nathan Plant Posted January 30, 2008 Author Posted January 30, 2008 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
IcemanND Posted January 31, 2008 Posted January 31, 2008 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", NewNameShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", NewName
Nathan Plant Posted January 31, 2008 Author Posted January 31, 2008 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.
Nathan Plant Posted February 1, 2008 Author Posted February 1, 2008 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now