freMea
Member
- Messages
- 38
- Reaction score
- 19
Hi nuhi,
I noticed a bug in NTLPromptCN.vbs. When the input dialog appear, if user click OK or Cancel or press Enter while the input field is blank (it can happens by accident and install process is then screwed), the PC name in unattend.xml will remain [Prompt]. Therefore, the setup will bug. Plus, the script doesn’t check if the user input is a valid PC name.
So I made a new script that fixes this and adds features:
Here is the script. I heavily tested it and it works as intended.
I noticed a bug in NTLPromptCN.vbs. When the input dialog appear, if user click OK or Cancel or press Enter while the input field is blank (it can happens by accident and install process is then screwed), the PC name in unattend.xml will remain [Prompt]. Therefore, the setup will bug. Plus, the script doesn’t check if the user input is a valid PC name.
So I made a new script that fixes this and adds features:
- more interactive and informative
- suggests a random 14 chars length PC name
- if user doesn’t custom a PC name, the suggested one is set even if Cancel is pressed or field is left blank
- prevents user from setting an invalid PC name, inform him and asks a new PC name until a valid one or none is set
Here is the script. I heavily tested it and it works as intended.
Code:
Option Explicit
Dim unattendFile, WshShell, fso, unattendFileObject, strContents
unattendFile = "C:\Windows\Panther\unattend.xml"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' Since this SCRIPT won’t do anything unless unattend.xml exists, check that first
If fso.FileExists(unattendFile) = False Then WScript.Quit
' If the unattend.xml is set to [Prompt] user for PC name, show input dialog
Set unattendFileObject = fso.OpenTextFile(unattendFile, 1)
strContents = unattendFileObject.ReadAll
strContents = InStr(strContents, "[Prompt]")
unattendFileObject.Close
If strContents > 0 Then Call Querry()
wscript.Quit
Private Sub Querry()
Dim answer, computerName, strDefName, strMsg
' This is default name and also used as placeholder (14 chars length = 8 general + 6 random alphanumrics chars)
strDefName = "PC-USER-"&RandomString(6)
strMsg = "Enter a computer name. Name must be unique on your local network."
' Loop until user enters a valid PC name or cancel or enters a blank name
Do While answer <> vbYes
computerName = UCase(InputBox(strMsg &vbCrLf&vbCrLf&"15 caractères maximum sans espace ni caractères spéciaux autre que" &vbCrLf& "tiret ( - )", "Nom du PC", strDefName))
' If users enters a blank name or click cancel, force the use of the default random PC name
If computerName = "" Then
computerName = strDefName
Exit Do
End If
' If user input is a valid PC name, ask him to confirm or modify
' else, show input again with an error message
If isValid(computerName) = True Then
answer = MsgBox("PC Name: " & computerName &vbCrLf&vbCrLf& "[NO] to modify", vbYesNo+vbQuestion, "Confirm PC Name")
Else
strMsg = "INVALID NAME, PLEASE START AGAIN"
End If
Loop
' Replace [Prompt] with computerName in unattend.xml
Set unattendFileObject = fso.OpenTextFile(unattendFile, 1)
strContents = unattendFileObject.ReadAll
strContents = Replace(strContents, "[Prompt]", computerName)
unattendFileObject.Close
Set unattendFileObject = fso.OpenTextFile(unattendFile, 2)
unattendFileObject.Write(strContents)
unattendFileObject.Close
WScript.Sleep 5000
WshShell.Run "%WINDIR%\System32\oobe\windeploy.exe", 0, True
End Sub
' =========================================================================
' HELPER FUNCTIONS
' =========================================================================
' Returns a custom length alphanumeric random string
Function RandomString(ByVal strLen)
Dim str, i
Const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
For i = 1 To strLen
Randomize
str = str & Mid( LETTERS, Int(Len( LETTERS ) * Rnd() + 1), 1 )
Next
RandomString = str
End Function
' Returns TRUE if string parameter is a valid PC Name
Function isValid(ByVal strValue)
Dim objRegEx
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.MultiLine = True
objRegEx.Pattern = "^[a-z0-9]+[a-z0-9-]*[a-z0-9]+$"
If Len(strValue) > 15 Then
isValid = False
Else
isValid = objRegEx.test(strValue)
End If
End Function