I had some struggles to get SCCM to set a computer name.
The below Script is to set a computer name during the Task Sequence.
In my environment, I use Hyper-V VM's and HP Laptops and Desktops.
Note:
1. For the below : I dont have VMware in my environment as we are using Hyper-V but this can be adjusted to your preference.
$MACAddress = Get-WmiObject Win32_NetworkAdapter -Filter "Name='Microsoft Hyper-V Network Adapter'" | Select MacAddress -Unique | ForEach {$_.MacAddress -replace ":",""}
So my VM's are being assigned hostnames as their MAC Addresses and removing the ":"
2. I noticed that Desktops, Workstations and VM's may have the same PCSystemType (1 or 3) so a condition was placed as per: ($DesktopTypes -contains $PCSystemType -and $ComputerModel -notmatch "Virtual Machine")
- So if the PCSystemType is 1 and if computer model MATCHES Virtual machine it wont take action and move to the next step and assign the computer name as MACAddress.
- If the PCSystemType is 1 and the device is NOT a virtual machine, it then adds the WKS prefix in front of the Serial number. IE: WKS5CG0000000
- For laptops it will check if the PCSystemType matches 2 or 8 and if true it will set the computer name as Prefix (LAP) and Serial number. IE: LAP5CG000000
More info on PCSystemType:
Specifies the type of the computer in use, such as laptop, desktop, or Tablet
docs.microsoft.com
3. Some computers may have their serial numbers longer than 15 characters. By design you cannot set a computer name longer than 15 characters. The script will shorten it.
Describes how to name computers, domains, sites, and organizational units in Active Directory.
docs.microsoft.com
Hopefully this can help someone out.
Place this before the Apply Operating System step.
Edit the TS --> Add--> General --> Run Powershell Script and paste the code below.
Ensure to select Bypass from the drop down within that step.
This is what I use:
SCRIPT
#Get Computer Info
$ComputerModel = Get-WmiObject -Class Win32_ComputerSystem | Select-Object Model
$MACAddress = Get-WmiObject Win32_NetworkAdapter -Filter "Name='Microsoft Hyper-V Network Adapter'" | Select MacAddress -Unique | ForEach {$_.MacAddress -replace ":",""}
$SerialNumber = (Get-WmiObject -Class Win32_BIOS).SerialNumber
$PCSystemType = (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType
$LaptopTypes = @(2,8)
$DesktopTypes = @(1,3) #Desktops, Workstations and VM's use this PCSystemType
$LaptopPrefix = "LAP"
$WorkstationPrefix = "WKS"
#Laptops, Desktops and Virtual Machines (Hyper-V)
if($LaptopTypes -contains $PCSystemType){
$NewName = "$LaptopPrefix$SerialNumber"
}
elseif($DesktopTypes -contains $PCSystemType -and $ComputerModel -notmatch "Virtual Machine"){
$NewName = "$WorkstationPrefix$SerialNumber"
}
else{$ComputerModel -match "Virtual Machine"
$NewName = "$MACAddress"
}
Write-Host "NewName: $NewName"
if($NewName.length -gt 15){
if($NewName -match "-"){$NewName = $NewName -replace "-",""}
#if name is still longer than 15 characters, truncate it to first 15 chars
if($NewName.Length -gt 15){
$NewName = $NewName[0..14] -join ''
Write-Host "Truncated Name: $NewName"
}
}
try{
(New-Object -COMObject Microsoft.SMS.TSEnvironment).Value("OSDComputerName") = "$NewName"
$OSDComputerName = (New-Object -COMObject Microsoft.SMS.TSEnvironment).Value("OSDComputerName")
if($OSDComputerName -eq $NewName){
Write-Host "OSDComputerName successfully set to: $NewName"
$ExitCode = 0
}
else{
throw "OSDComputerName variable value ($OSDComputerName) does not match the calculated name ($NewName)"
}
}
catch{
Write-Error $_
$ExitCode = $_.Exception.hresult
}
Write-Host "== End Script - SetComputerName.ps1 =="
#[environment]::Exit($ExitCode)