Generate computer names based on folder content
Last week I got an email from a fellow deployment geek wanting to generate computer names based on what files that were in a folder… Every time they deployed a machined they had a script that saved the machine name to a folder, and he wanted a routine that figured out the next available name. Well, here it is…
Update 2014-08-26: Added info about how to create the text file in the end of the deployment.
For this to work you need to have a folder in your deployment share, named computers, with a similar content. As you can can see the folder holds names from computers already deployed, and the next available computer name should be MYPC0006, right?
Folder structure in the deployment share.
All that was needed was a short userexit script (VBScript) that enumerated the files in the folder, but because the FileSystemObject in VBScript does do sorting, I had to populate a list, and then sort that one. Below you find the GetNextComputerName.vbs script, and the CustomSettings.ini lines that calls it. You need to do is copy the GetNextComputerName.vbs script to your deployment share scripts folder, and modify your CustomSettings.ini file.
Note: You can obviously add code to also write the new name to the folder, but since they already have that function in place, I skipped that part.
The “code” in CustomSettings.ini.
The GetNextComputerName.vbs script.
Function UserExit(sType, sWhen, sDetail, bSkip)
UserExit = Success
oLogging.CreateEntry "------------ Initialization USEREXIT:GetNextComputerName.vbs -------------", LogTypeInfo
Set list = CreateObject("ADOR.Recordset")
list.Fields.Append "name", 200, 255
For Each oFile In oFSO.GetFolder(oEnvironment.Item("DEPLOYROOT") & "\Computers").Files
list("name").Value = oFile.Name
list.Sort = "Name ASC"
Do Until list.EOF
sLastComputer = list("name").Value
oLogging.CreateEntry "Last Computer Deployed was: " & sLastComputer, LogTypeInfo
sNextNumber = Mid(sLastComputer,5,4) + 1
GetNextComputerName = "MYPC" & right(sNextNumber + 10000,4)
oLogging.CreateEntry "Next Computer Name is: " & GetNextComputerName, LogTypeInfo
oLogging.CreateEntry "------------ Departing USEREXIT:GetNextComputerName.vbs -------------", LogTypeInfo
The next available computer name during deployment.
Here is how you configure the task sequence to write the new name, once it completes. Just add a Run Command Line action with the following command.
PowerShell.exe -ExecutionPolicy Bypass -Command "New-Item %DEPLOYROOT%\Computers\%OSDCOMPUTERNAME%.txt -type file"
Happy Deployment / Johan