- Scripting the Computer Name
- Generating a Computer Name with Script
- Listing of COMPNAME.VBS
Listing of COMPNAME.VBS
'********************************************** '*** '*** File: COMPNAME.VBS '*** Created: October 2000 '*** Version: 1.0 '*** '*** Main Function: Generate INF file to set '*** computer name based on WMI query of '*** SerialNumber FROM Win32_SystemEnclosure '*** '*** Copyright (c) 2000 Jeffrey A. Ferris '*** Ferris Technology Network '*** jeff@ferristech.net '*** '*** No warranty, express or implied. '*** Code provided strictly as an example. '*** Use at your own risk. '*** '*** Use of this file requires acceptance of '*** disclaimer at http://www.ferristech.net/win2k/disclaimers.html '********************************************** Dim strTagID, strComputerNameLine, flgOKName, strOutputFile, objOutputFile strOutputFile = "sysname.inf" '********************** '*** Runs the routines '********************** InitOutputFile GetTagID ' Pull the serial number / service tag 'CheckTagID ' check the returned values against some rules wscript.echo strComputerNameLine PrintINF '**************************************************************** '*** '*** Pull the serial number from WMI '*** Note that this can grab MAC addresses from docking stations '*** '**************************************************************** Sub GetTagID Set objcol = GetObject("WinMgmts:/root/cimv2").InstancesOf("Win32_systemenclosure") ' While flgOKName = 0 For Each i in objcol wscript.echo i.Serialnumber strTagID = i.Serialnumber If flgOKName = 0 Then CheckTagID If flgOKName = 1 Then Exit For Next If flgOKName = 0 Then AskForID End Sub '******************************************************************************** '*** '*** Run validity checks against the value returned from GetTagID function. '*** '*** In this case, I'm looking only for a 5-, 6-, or 7-character service tag. '*** (this script works well on Dell computers) '*** If there is no tag, I pop up an input box for the user to manually '*** enter the computer name. '*** '*** You can do any sort of validity checking you want from within this module. '*** '*** The strComputerNameLine is the value that will be substituted into '*** your *.inf file. If you want to use the returned identifier as '*** part of an algorithm in computing a computer name, you can '*** manipulate the value for the strComputerNameLine variable. '*** '******************************************************************************** Sub CheckTagID flgOKName = 0 Select Case Len(strTagID) Case 5 strComputerNameLine = "Computername = " & strTagID flgOKName = 1 Case 6 strComputerNameLine = "Computername = " & strTagID flgOKName = 1 Case 7 strComputerNameLine = "Computername = " & strTagID flgOKName = 1 Case 8 strComputerNameLine = "Computername = " & strTagID flgOKName = 1 Case Else 'no good. End Select End Sub Sub AskForID If flgOKName = 0 Then Do Until strComputerName <> "" strComputerName=InputBox("Please enter a valid computer name." _ & Chr(10) & "You should use the 5 to 7 character" _ & Chr(10) & "machine Service Tag." & Chr(10) _ & Chr(10) & "If the Service Tag is not available," _ & Chr(10) & "please check with the Network Administrator" _ & Chr(10) & "to determine the proper naming convention.", _ "Service Tag Unavailable.") Loop strComputerNameLine = "Computername = " & strComputerName flgOKName = 1 End If End Sub Sub PrintINF 'This section contains the lines that will output as SYSNAME.INF. 'Modify as needed. WriteLine "[UserData]", objOutputFile WriteLine strComputerNameLine, objOutputFile WriteLine "", objOutputFile WriteLine "[Networking]", objOutputFile WriteLine "InstallDefaultComponents = Yes", objOutputFile WriteLine "", objOutputFile WriteLine "[Identification]", objOutputFile WriteLine "JoinWorkgroup=WORKGROUP", objOutputFile WriteLine "", objOutputFile WriteLine "[NetAdapters]", objOutputFile WriteLine "Adapter1 = params.Adapter1", objOutputFile WriteLine "", objOutputFile WriteLine "[params.Adapter1]", objOutputFile WriteLine "INFID = *", objOutputFile WriteLine "", objOutputFile WriteLine "[NetProtocols]", objOutputFile WriteLine "MS_TCPIP = params.MSTCPIP", objOutputFile WriteLine "MS_PPTP = params.MSPPTP", objOutputFile WriteLine "MS_L2TP = params.MSL2TP", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MSTCPIP]", objOutputFile WriteLine "AdapterSections = params.MSTCPIP.Adapter1", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MSTCPIP.Adapter1]", objOutputFile WriteLine "SpecificTo = Adapter1", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MSPPTP]", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MSL2TP]", objOutputFile WriteLine "", objOutputFile WriteLine "[NetClients]", objOutputFile WriteLine "MS_MSClient = params.MS_Client", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MS_CLIENT]", objOutputFile WriteLine "", objOutputFile WriteLine "[NetServices]", objOutputFile WriteLine "MS_Server = params.MSFP", objOutputFile WriteLine "", objOutputFile WriteLine "[params.MSFP]", objOutputFile objOutputFile.Close End Sub '******************************************************************** '* '* Sub WriteLine() '* Purpose: Writes a text line either to a file or onscreen. '* Input: strMessage the string to print '* objFile an output file object '* Output: strMessage is either displayed onscreen or written to a file. '* '******************************************************************** Sub WriteLine(ByRef strMessage, ByRef objFile) If IsObject(objFile) then 'objFile should be a file object objFile.WriteLine strMessage Else WScript.Echo strMessage End If End Sub Sub InitOutputFile If strOutputFile = "" Then objOutputFile = "" Else 'Create a file object set objFileSystem = CreateObject("Scripting.FileSystemObject") If Err.Number then Print "Error 0x" & CStr(Hex(Err.Number)) & " opening a filesystem object." If Err.Description <> "" Then Print "Error description: " & Err.Description & "." End If Exit Sub End If 'Whack the file if it already exists '*** The following three lines are remarked out because I don't want to '*** inadvertently kill anyone's files. Re-enable at your own risk. ' If (objFileSystem.FileExists(strOutputFile)) Then ' objFileSystem.DeleteFile(strOutputFile) ' End If 'Open the file for output set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, 8, True) If Err.Number then Print "Error 0x" & CStr(Hex(Err.Number)) & " opening file " & strOutputFile If Err.Description <> "" Then Print "Error description: " & Err.Description & "." End If Exit Sub End If End If End Sub
You can modify the script in any way to pull or prompt for information that will meet your corporate naming structure. If the computer service tag doesn't fit with your corporate naming structure, some other potential query options are listed in the SMBIOS-to-CIM mappings table, at http://www.microsoft.com/TechNet/sms/getassett.asp - c.
To use the resulting SYSNAME.INF file, you'll need the NETSET.EXE utility from the Windows 2000 Server Resource Kit CD. A complete list of tools included with the Windows 2000 Server Resource Kit can be found at http://www.microsoft.com/windows2000/library/resources/reskit/rktour/server/S_tools.asp.
In another article, we'll explore NETSET.EXE a little further, using it to configure network settings and join the domain. In the fourth installment in this series, I'll discuss tying NETSET.EXE into the automated installation process.