Forums on Intune, SCCM, and Windows 11

Welcome to the forums. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your topics and posts, as well as connect with other members through your own private inbox!

PENDING change device name with sccm

  • Thread starter Thread starter Alius
  • Start date Start date
  • Replies Replies 7
  • Views Views 13K

Alius

Member
Messages
5
Reaction score
0
Points
1
Hi GUYS
I search the web for a way to rename client with sccm; but cant find any thing
any idea?
Thanks for your reply
 
Hi GUYS
I search the web for a way to rename client with sccm; but cant find any thing
any idea?
Thanks for your reply
What do you mean? The name listed within CM is the computer name. Are you trying to change the computer name or something else?
 
Hi
yes i want to change computer name on workstations from sccm
There is no built-in way to do this with CM. At best you can try to do it with powershell and running the script remotely but that will require permissions and reboot.

You can try to vote this idea up https://ideas.recastsoftware.com/ideas/RS-I-160 But I'm sure that this is for clients only.
 
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:

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.




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)
 
Last edited:
I also wanted to add that I am also using this script to set hostnames for my AAD joined devices which are being managed by Intune.
So for that the same script is being used but with slight adjustments.

Script for AAD joined device managed by Intune:
#Set Computer Name for AAD Devices
#If Laptop: Assigns the name as LAP followed by Serial# - IE: LAP5CG12223344
#If Desktop: Assigns the name as WKS followed by Serial# - IE: WKS5CG12223344
#If Virtual: Assigns the name as the MACADDRESS.

$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)
$LaptopPrefix = "LAP"
$WorkstationPrefix = "WKS"

#Laptops / Desktops / Virtual Machines

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

}

Rename-Computer -NewName $NewName
 
Hello,

maybe worth an info.
The PSComputerName is still the old one even after a successful name change.
Only when the CM client is reinstalled it changes.
 

Forum statistics

Threads
7,164
Messages
27,964
Members
18,257
Latest member
sujata

Trending content

Back
Top