There are countless ways to set a hostname in SCCM OSD Task Sequence: through variables, queries or manipulating various files. You can automate hostname assignment and derive it from, for example, a serial number or a MAC address, however sometimes it is necessary to prompt user to enter hostname (e.g. for hostname to match asset tag affixed on the device). In this little post I would like to present a simple yet effective way of prompting user to enter host name and validate their input before proceeding.
In this example I will focus only ensuring that hostname is within constrains of both DNS and NetBIOS conventions. This essentially means that hostname needs to be between 2 to 15 characters long and can contain only alphabetical characters (A-Z, upper and lower case), numeric characters (0-9) and the minus sign (-). More details on naming conventions constrains can be found on https://support.microsoft.com/en-gb/kb/909264.
In order to achieve our goal the script will perform following tasks:
- Close Task Sequence progress window
- Prompt user to enter desired hostname
- Validate length of entered string (display warning message and return to input screen if entered hostname doesn’t meet the criteria)
- Validate characters of entered string using of Regular Expressions in search for disallowed characters (display warning message and return to input screen if entered hostname doesn’t meet the criteria)
- If entered hostname meets defined requirements, prompt user to confirm they are happy to proceed
- Set OSDComputerName Task Sequence variable
The script will of course loop until valid hostname is provided and confirmed. That is enough talk, as it is actually easier done than said.
Option Explicit Dim TSenv, oTSProgressUI, strComputerName, OSDComputerName, bConfirmed, bInvalidCharactersFound, bHostnameValid, iMinLength, iMaxLength, vAnswer, objRegEx, strAllowedCharacters, item, objMatches ' NetBIOS hostname length constrains: 1-15 characters ' DNS hostname length constrains: 2-63 characters ' Using pattern that will satisfy both standards iMinLength = 2 iMaxLength = 15 strAllowedCharacters = "[^a-zA-Z0-9-]" 'Hide Task Sequence progress dialog Set TSenv = CreateObject("Microsoft.SMS.TSEnvironment") Set oTSProgressUI = CreateObject("Microsoft.SMS.TSProgressUI") oTSProgressUI.CloseProgressDialog() bConfirmed = false bHostnameValid = false Do ' Prompt user for input strComputerName = Inputbox("Please enter the desired hostname.","Set computer name") bInvalidCharactersFound = false If Len(strComputerName) < iMinLength Then ' Display warning message if string is too short vAnswer = MsgBox ("Hostname needs to contain at least " & iMinLength & " characters.", vbOKOnly + vbExclamation,"Invalid hostname") bHostnameValid = false ElseIf Len(strComputerName) > iMaxLength Then ' Display warning message if string is too short vAnswer = MsgBox ("Hostname needs to contain no more than " & iMaxLength & " characters.", vbOKOnly + vbExclamation,"Invalid hostname") bHostnameValid = false Else ' Execute regular expression and look for invalid characters Set objRegEx = New RegExp objRegEx.Global = True objRegEx.Pattern = strAllowedCharacters Set objMatches = objRegEx.Execute(strComputerName) For Each item in objMatches bInvalidCharactersFound = True Next If bInvalidCharactersFound = false Then bHostnameValid = true Else ' Display warning message if string contains invalid characters vAnswer = MsgBox ("Hostname can contain only alphabetical characters (A-Z), numeric characters (0-9) and the minus sign (-).", vbOKOnly + vbExclamation,"Invalid hostname") bHostnameValid = false End If End If If bHostnameValid = True Then ' Prompt user to confirm they are happy with their input for hostname vAnswer = MsgBox ("Are you sure you want to build this machine with the following hostname?" & vbCrLf & vbCrLf & strComputerName, vbYesNo + vbQuestion,"Confirmation required") If vAnswer = vbYes Then bConfirmed = true End If End If Loop Until bConfirmed = true ' Set OSDComputerName Task Sequence variable TSenv("OSDComputerName") = strComputerName
All that needs to be done now is to save the file as “Set_OSDComputerName.vbs”, create new package (without any programs) with the script and distribute the content. Once that is done, at the beginning of your Task Sequence add Run Command Line step and call it “Prompt for hostname”). Under Command Line field enter cscript.exe Set_OSDComputerName.vbs. Next, select Package tick box and point it to your package holding VBScript.