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.

Preparing

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?

image
Folder structure in the deployment share.

Solution

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.

[Settings]
Priority=Default

[Default]
UserExit=GetNextComputerName.vbs
OSDComputerName=#GetNextComputerName()#

The GetNextComputerName.vbs script.

Function UserExit(sType, sWhen, sDetail, bSkip) 
 
    UserExit = Success 
 
End Function
 
Function GetNextComputerName()
 
    oLogging.CreateEntry "------------ Initialization USEREXIT:GetNextComputerName.vbs -------------", LogTypeInfo
 
    Set list = CreateObject("ADOR.Recordset")
    list.Fields.Append "name", 200, 255
    list.Open
 
    For Each oFile In oFSO.GetFolder(oEnvironment.Item("DEPLOYROOT") & "\Computers").Files
      list.AddNew
      list("name").Value = oFile.Name
      list.Update
    Next
 
    list.Sort = "Name ASC"
 
    list.MoveFirst
    Do Until list.EOF
      sLastComputer = list("name").Value
      list.MoveNext
    Loop
 
    list.Close
     
    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
 
End Function
image

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"
image

Happy Deployment / Johan

About the author

Johan Arwidmark

5 1 vote
Article Rating
Subscribe
Notify of
guest
13 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Ryze
Ryze
11 months ago

Hi, although this is a really old post this has been useful for my MDT environment. The only issue I've got which is similar to @Kaney777's issue is that the script doesn't allow more than 4 characters for the naming convention.

Similar to @Kaney777 my naming convention is ROOM10WSXXX how would you alter the script to allow more than 4 characters on line 34:

GetNextComputerName = "MYPC" & right(sNextNumber + 10000,4)
Ryze
Ryze
10 months ago

Thanks Johan! That's done the trick 🙂

Admin
Admin
8 years ago

Contact me offline (send me an email, info on the about page), and I can help

/ Johan

Kimzi
Kimzi
8 years ago

Hi, Johan! I've done some more testing, tried converting your script into a wsf script, did not help.So I did create a tiny test environment to see what the heck is going wrong, when I looked at the log files, ztigather and bdd.log I did see that the user exit command wasn't executed until after the OSDComputerName variable was processed.I put the user exit command in a new priority. Now it does execute before OSDComputerName and the lines I get is: Initialization USEREXIT:GetNextComputerName.vbsLast Computer Deployed was : MYPC0012 Property OSDCOMPUTERNAME is now = #GetComputerName()# Which means it does read the… Read more »

Kimzi
Kimzi
8 years ago

Hi again, Johan!

I did try using a custom variable, this resulted in the same problem, it did only show #GetNextComputerName()#.
I will have a look at the examples you linked to, hopefully I'll figure it out.
Just can't understand why it does not work, as it clearly works for you.

Best Regards
Kimzi

Admin
Admin
8 years ago

Hi Kimzi,

If you want to force the OSDComputerName value you can break out the userexit script code into to a normal MDT script and call it frm the task sequence (including calls to ZTIUtility etc).

Here are a few samples: https://deploymentresearch.com/Research/tabid/62/EntryId/89/TechEd-WCA-B305-VBScript-and-PowerShell-wrappers-for-MDT-ConfigMgr.aspx

Another options is using a custom variable to get the name, like MyOSDComputerName=#GetNextComputerName()# and then add a set task sequence variable action that sets OSDComputerName=%MyOSDComputerName%

/ Johan

/ Johan

/ Johan

Kimzi
Kimzi
8 years ago

One more thing :

Removing the line : UserExit=GetNextComputerName.vbs will give the computer a MININT name.
so the script's just giving the function name?

Kimzi
Kimzi
8 years ago

Hi again, Johan!

Thank you for the reply.

I've tried to troubleshoot the issue, I do have a custom DeployWiz_SelectTS.vbs, the wizard does read the variables after selecting task, I tried moving the variables to [Default] instead, no change.
I tried replacing the DeployWiz_SelectTS.vbs with the original one, also changed to "Priority=Default" still no change.
Also changing OSDComputerName=#GetNextComputerName()# to OSDComputerName=Comp001 does work, both under [Default] and [TASK1].
I've changed nothing in ur script, and I also use the folder "Computers".

Any ideas?

I truly appreciate the help.

Best Regards
Kimzi

Admin
Admin
8 years ago

Hi Kimzi,

The deployment wizard does not re-read the variables when you select a task sequence. Move the UserExit to [Default] instead.

/ Johan

Kimzi
Kimzi
8 years ago

Hi, Johan!

Great article, this is just what I've been looking for.
Though I do have one problem, the same as the user above actually.

My CS.ini looks like this :

[Settings]
Priority=TaskSequenceID, Default

[Default]

[TASK1]
UserExit=GetNextComputerName.vbs
OSDComputerName=#GetNextComputerName()#

When running a test (ZTIGather) I can see that the script is being run but nothing is gathered from it.
Do you have any ideas as to why this might happen?

Best Regards
Kimzi

Admin
Admin
8 years ago

You probably didn't configure your customsettings.ini file correctly, and as far as writing the computer name back, I just updated this blog post with that info.

/ Johan

Kaney777
Kaney777
8 years ago

Hi, this looks awesome however I am doing something wrong and wonder if you can point me in the right direction.

I have copied the script as they and changed "MYPC" to "TPCWS" but when I get to the LTI Computer Details screen the Computer name is populated with #GetNextComputerName()#

My naming convention is TPCWSxx, at the moment my computer folder has one file called tpcws41.txt

Also any chance of a blog entry not skipping the code to write the new computer name back into the folder ? 🙂

Cheers,

Kane.


>