Enjoy Sharing

Script to Assign Computer Name from Asset Tag with Validation to be used by SCCM Task Sequence

I’m not usually a script writer (especially when it comes to VBS) but I’m surprised how I got from a small script to basically prompt the user to enter a computer name to something as big as this.

I have been asked to automatically assign the computer name during a Windows deployment. Not just the common task to prompt the user to enter a computer name which you can do pretty easily just by configuring a task sequence variable to the target collection you’re deploying the task sequence to but to assign the computer name with the Asset Tag value found in the BIOS.

In my situation all computers have been assigned with a customised Asset Tag value (either from the manufacturer’s factory or using a separate tool) so what needs to be done is to read that value from the Asset Tag and assign it as the computer name in a Task Sequence. There’s no way I can do it straight out of a task sequence so a script has to be written for it.

So what I’ve done with the script is:-

  • Read the value of the Asset Tag in the BIOS through the SMBIOSAssetTag from Win32_SystemEnclosure class
  • Validate the value first to see if it contains only all the correct characters and not symbols
  • Read the value of the chassis type through the ChassisType also from the Win32_SystemEnclosure class
  • Depending on the chassis type number read from the BIOS, assign it as either Desktop, Laptop or Unknown
  • Based on whether it is a Desktop or Laptop, run another validation to see if it conforms with the correct naming convention which in my case has a prefix of either “PC-“ or “LT-“ respectively
  • Last 5 characters is validated to see that it only contains numbers. So a full computer name will look something like e.g. “PC-12345” or “LT-98765”.
  • If the value doesn’t conform to the naming convention, then prompt for the user to enter the correct computer name
  • Run the check after to validate the values entered by the user that it conforms to naming conventions and prompt again until a valid value is entered
  • Only if it passes all of the above, then proceed to assign the value to the OSDComputerName

Phew…that was quite a lengthy one. And quite a lengthy one it is. Attached below is the script that makes all this work.

 

===Start Script===

Dim objOSD, objRegEx
Dim Matches, Match
Dim strPattern, strInputBox, strReason
Dim boolLength, boolValid, StrType

Set objOSD = CreateObject("Microsoft.SMS.TSEnvironment")
Set objRegEx = New RegExp
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure",,48)
For Each objItem in colItems
OSDComputername = objItem.SMBIOSAssetTag
OSDComputername = Left(OSDComputerName,8)
Next

‘ Define valid patterns as and character not in (A-Z, 0-9, or -)
strPattern = "[^a-zA-Z0-9-]"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
    ("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
    For  Each strChassisType in objChassis.ChassisTypes
        Select Case strChassisType

            Case 3
                  StrType = "Desktop"
            Case 4
                   StrType = "Desktop"
            Case 6
                   StrType = "Desktop"
            Case 7
                  StrType = "Desktop"
            Case 8
                StrType = "Laptop"
            Case 9
                 StrType = "Laptop"
            Case 10
                  StrType = "Laptop"
            Case 11
                  StrType = "Laptop"
            Case 12
                   StrType = "Laptop"
            Case 14
                  StrType = "Laptop"
            Case 15
                  StrType = "Laptop"
            Case Else
        StrType = "unknown"
            End Select
    Next
Next
If StrType = "Desktop" then

  Do
   ‘ Check ComputerName – must comply to the naming standard
   If Len(OSDComputerName) <> 8 Then
    boolLength = False
   ElseIf Left(OSDComputerName, 3) <>  "PC-" AND Left(OSDComputerName, 3) <> "EV-"  Then
    boolLength = False
   ElseIf IsNumeric(Right(OSDComputerName, 5)) = False Then
        boolLength = False
   Else
    boolLength = True
   End If

  If boolLength = "False" Then
   strReason = ""
   strInputBoxA = InputBox("Enter desired machine name:" & VbCrLf & VbCrLf & "Names must start with PC- and only numbers in the last 5 characters " & VbCrLf &  "Current PC number is :-(" & OSDComputername &")","Machine Name",,60,60)
   If strInputBoxA = "" Then TemplateQuit(0)
   ‘COVERT STRING TO UPPERCASE
   OSDComputerName = UCase(strInputBoxA)
  End If

   ‘ Check character validity
   boolValid = True
   ‘ Return all matches for invalid characters
   objRegEx.Global = True
   objRegEx.Pattern = strPattern
   ‘ Generate collection of matches
   Set Matches = objRegEx.Execute(strInputBox)
   ‘ Check for matches on invalid characters
   For Each Match In Matches
    boolValid = False
   Next
  Loop While boolLength = "False"
End If
If StrType = "Laptop" then

  Do
   ‘ Check ComputerName – must comply to the naming standard
   If Len(OSDComputerName) <> 8 Then
    boolLength = False
   ElseIf Left(OSDComputerName, 3) <> "LT-" AND Left(OSDComputerName, 3) <> "EV-"  Then
    boolLength = False
   ElseIf IsNumeric(Right(OSDComputerName, 5)) = False Then
        boolLength = False
   Else
    boolLength = True
   End If

  If boolLength = "False" Then
   strReason = ""
   strInputBoxA = InputBox("Enter desired machine name:" & VbCrLf & VbCrLf & "Names must start with LT- and only numbers in the last 5 characters " & VbCrLf &  "Current PC number is :- (" & OSDComputername &")","Machine Name",,60,60)
   If strInputBoxA = "" Then TemplateQuit(0)
   ‘COVERT STRING TO UPPERCASE
   OSDComputerName = UCase(strInputBoxA)
  End If

   ‘ Check character validity
   boolValid = True
   ‘ Return all matches for invalid characters
   objRegEx.Global = True
   objRegEx.Pattern = strPattern
   ‘ Generate collection of matches
   Set Matches = objRegEx.Execute(strInputBox)
   ‘ Check for matches on invalid characters
   For Each Match In Matches
    boolValid = False
   Next
  Loop While boolLength = "False"
End If
If StrType = "Unknown" then

  Do
   ‘ Check ComputerName – must comply to the naming standard
   If Len(OSDComputerName) <> 8 Then
    boolLength = False
   ElseIf Left(OSDComputerName, 3) <> "LT-" AND Left(OSDComputerName, 3) <> "PC-" AND Left(OSDComputerName, 3) <> "EV-" AND Left(OSDComputerName, 1) <> "V" Then
    boolLength = False
   ElseIf IsNumeric(Right(OSDComputerName, 5)) = False Then
        boolLength = False
   Else
    boolLength = True
   End If

  If boolLength = "False" Then
   strReason = ""
   strInputBoxA = InputBox("Enter desired machine name:" & VbCrLf & VbCrLf & "Names must start with PC- or LT- and only numbers in the last 5 characters " & VbCrLf &  "Current PC number is :- (" & OSDComputername & ")","Machine Name",,60,60)
   If strInputBoxA = "" Then TemplateQuit(0)
   ‘COVERT STRING TO UPPERCASE
   OSDComputerName = UCase(strInputBoxA)
  End If

   ‘ Check character validity
   boolValid = True
   ‘ Return all matches for invalid characters
   objRegEx.Global = True
   objRegEx.Pattern = strPattern
   ‘ Generate collection of matches
   Set Matches = objRegEx.Execute(strInputBox)
   ‘ Check for matches on invalid characters
   For Each Match In Matches
    boolValid = False
   Next
  Loop While boolLength = "False"
End If

objOSD("OSDComputerName") = OSDComputerName

===End Script===

 

 

Enjoy!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s