SCCM 2012 R2 – Serial No as Computer Name in OSD

      Comments Off on SCCM 2012 R2 – Serial No as Computer Name in OSD

In this post we’ll be renaming the computer based on Serial Number. First we need to determine what computer model the OS is being deployed on. Secondly we want to name the computer by using the serial number.

The script

We begin with populating variables $SerialNumber data from WMI. We create a new COM object from the Microsoft.SMS.TSEnvironment class. The value of $OSDComputerName will then be put into the newly created COM object.

——————–
#Script for OSDComputerName

$SerialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
$OSDComputerName = $SerialNumber
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value(“OSDComputerName”) = “$OSDComputerName”
———————-

Create a Package with the script

1. Save the above script to a .ps1 file, name it OSDComputerName.ps1 and store it in your folder structure for packages and applications.
2. Go to Software Library, Packages and create a new package.
3. Give the Package a name, e.g. OSDComputerName. Check This package contains source files and enter the location where you saved the script. Remember that it has to be an UNC path. Click Next.
4. Choose Do not create a program and click Next.
5. Click Next to confirm the settings.
6. Click Close when the wizard is complete.
7. Distribute the Package to your DP’s.


Using the script in a Task Sequence

1. Edit your OSD Task Sequence and add a New group after the Apply Drivers step. It’s important that you add the new group before the Apply Windows Settings and Apply Windows Network Settings steps.
2. Name the new group like OSDComputerName.
3. In the new group, create a new Run Command Line step and call it Set PowerShell Execution Policy. Enter the following in the Command line field:
“powershell.exe -noprofile -command “Set-ExecutionPolicy Bypass LocalMachine” -force”

4. In the new group, create a new Run Command Line step and call it OSDComputerName.ps1. Enter the following in the Command line field:
“powershell.exe -noprofile -file OSDComputerName.ps1”

5. Check Package and select your newly create package OSDComputerName. (Picture below says OSDChangeName, taken from an old package.)

6. In the new group, create a new Run Command Line step and call it Revert PowerShell Execution Policy. Enter the following in the Command line field:
“powershell.exe -noprofile -command “Set-ExecutionPolicy RemoteSigned LocalMachine” -force”

7. You should now have a new group with three steps. Make sure they’re arranged like below.

That’s all! Run your Task Sequence on a lab client and check out the results.

Note:
To run this script successfully, in your boot images following components should added.
– Microsoft .NET (WinPE-NetFx)
– Windows PowerShell (WinPE-PowerShell)

If you haven’t added these component follow the step to add.

1. Right click on your boot image and choose Properties.
2. Go to the Optional Components tab and click on the yellow star.
3. Add the following components:
– Microsoft .NET (WinPE-NetFx)
– Windows PowerShell (WinPE-PowerShell)
4. Click OK and update your distribution points.

Once they’re updated, rerun the task sequence and the script should run perfectly fine!