SCCM – PowerShell, VBScript – Name Computer in Task Sequence

email me

This is how you automate the process of naming computers during OSD.

PowerShell Method

First, create an application package, but choose Do not create a program, instead of Standard for the program type.


OSDComputerName.ps1

$SerialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
$OSDComputerName = "ABC-" + $SerialNumber
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
#$TSEnv.Value("$env:computername") = "$OSDComputerName"
$TSEnv.Value("OSDComputerName") = "$OSDComputerName"

#Rename-Computer -ComputerName "$env:computername" -NewName "$OSDComputerName"
#Rename-Computer -ComputerName "OSDComputerName" -NewName "$OSDComputerName"

 

Next, in the Task Sequence, create three Run Commands:

#1
powershell.exe -noprofile -command “Set-ExecutionPolicy Bypass LocalMachine” -force

#2
powershell.exe -noprofile -file OSDComputerName.ps1
* make sure you link an empty package (an application package with no program) to the OSDComputername.ps1

#3
powershell.exe -noprofile -command “Set-ExecutionPolicy RemoteSigned LocalMachine” -force

* Note, this method does require that PowerShell be enabled in the boot.wim

 

VBScript Method

Make an item in Post Setup pointing to this script. Add a SCCM Restart.

on error resume next

Dim computername

strComputer = "."

Set objWMIservice = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

set colitems = objWMIservice.ExecQuery("Select * from Win32_BIOS",,48)

For each objitem in colitems
'Wscript.echo "Dell Service Tag: " & objitem.serialnumber
computername = objitem.serialnumber

Next

'returns machine model number only
'PRIMARY MODEL DETECTION
Set objWMI = GetObject("winmgmts:")
Set colSettings = objWMI.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colSettings
LaptopModel = Trim(objComputer.Model)
Next

Select Case LaptopModel

Case "HP EliteBook 840 G3"
strModel = "8403"

Case "HP EliteBook 840 G2"
strModel = "8402"

Case "HP EliteBook 840 G1"
strModel = "8401"

Case "HP EliteBook Folio 9480m"
strModel = "9480"

Case "HP EliteBook Folio 9470m"
strModel = "9470"

Case "HP EliteBook Folio 9460m"
strModel = "9460"

Case "HP EliteBook 8470p"
strModel = "8470"

Case "HP EliteBook 8460p"
strModel = "8460"

Case "HP EliteBook 8450p"
strModel = "8450"

Case "HP EliteBook 8440p"
strModel = "8440"

Case "HP EliteBook 6930p"
strModel = "6930"

Case "HP EliteBook 2530p"
strModel = "2530"

Case "HP EliteBook 2540p"
strModel = "2540"

Case "HP Compaq dc7900 Small Form Factor"
strModel = "7900"

Case "HP Compaq 8000 Elite SFF PC"
strModel = "8000"

Case "HP Compaq 8200 Elite SFF PC"
strModel = "8200"

Case "OptiPlex 755"
strModel = "755"

Case "OptiPlex 745"
strModel = "745"

Case "Latitude D630"
strModel = "630"

Case "Latitude D620"
strModel = "620"

Case "Latitude D430"
strModel = "430"

End Select

'SECONDARY MODEL DETECTION
'used for models not explicitly defined
if strModel = "" then
'msgbox "no model was detected"
myLength = Len(LaptopModel)

For i = 1 To myLength
If Asc(Mid(LaptopModel, i, 1)) <> 32 Then
If Asc(Mid(LaptopModel, i, 1)) >= 48 And Asc(Mid(LaptopModel, i, 1)) <= 57 Then
myNumber = myNumber & Mid(LaptopModel, i, 1)
End If
Else
'msgbox("no numeric")
End If
Next
'msgbox(myNumber)
strModel = myNumber
end if

'testing only
'msgbox strModel

'renames machine with model number - service tag
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers

err = objComputer.Rename(strModel & "-" & computername)

Next

WScript.Quit(0)

 

 

Notes

Command Line

netdom renamecomputer member /newname:member1.contoso.com /userd:administrator

 

PowerShell

Rename-Computer -ComputerName “$env:computername” -NewName “NewComputerName”

Rename-Computer -ComputerName “OSDComputerName” -NewName “NewComputerName”


Remotely Rename

$TargetComp=Read-Host -Prompt “Enter the Name of the Computer you want to change the name of “
$Credential=Get-Credential
$computerName = GWMI Win32_ComputerSystem -computername $TargetComp -Authentication 6
Write-host “Current Computer Name is ” $computerName
$name = Read-Host -Prompt “Please Enter the ComputerName you want to use.”
Write-host “New Computer Name ” $Name
$Go=Read-Host -prompt “Proceed with computer name change? (Y / N)”
If(($Go-eq”Y”)-or($Go-eq”y”))
{
$computername.Rename($name,$credential.GetNetworkCredential().Password,$credential.Username)
}
$Reboot=Read-host -Prompt “Do you want to restart the computer? (Y / N)”
If(($Reboot-eq”Y”)-or($Reboot-eq”y”))
{
restart-computer -computername $TargetComp
}