Prompt to Rename Computer During Task Sequence via Powershell

Here is my powershell script to prompt to rename computer during SCCM OSD Task Sequence.

It will first auto popular machine name according to serial number, and then ask user to rename computer. If user do not do anything, the pop out window will auto close within 30 seconds.

# Smart Computer Re-name System
# Rui Qiu
# 8/9/2018, Last edit 8/25/2018
# v2.0

#Hide the progress dialog
$TSProgressUI = new-object -comobject Microsoft.SMS.TSProgressUI
$TSProgressUI.CloseProgressDialog()

# Check Hardware Type & Model
$hardwaretype = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty PCSystemType
$model = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model
If ($model -like “HP*Z*”) {$pre = “Z”}
Else {
If ($hardwaretype -ne 2) {$pre = “D”}
Else {$pre = “L”}
}
Write-Host “DEBUG – Machine Type is $pre”

# Check Serial Number
$SerialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
Write-Host “DEBUG – Serial Number is $SerialNumber”

# Prepare New Computer Name
$OSDComputerName = $pre + “-” + $SerialNumber
Write-Host “DEBUG – New Default Computer Name will be $OSDComputerName”

# Ask if user wants to rename
Add-Type -AssemblyName PresentationCore,PresentationFramework
$MessageBody = “$OSDComputerName will be the machine name, do you want to change it?”
$MessageTitle = “Computer Name”
$Prompt = new-object -comobject wscript.shell
$Box = $Prompt.popup(“$MessageBody`n”,30,”$MessageTitle”,4)

switch ($Box){

‘6’{
[void][Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’)
$title = ‘New Computer Name’
$msg = ‘Please Enter the ComputerName you want to use:’
$name = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
[System.Windows.MessageBox]::Show(“New Computer Name will be $name”)
$OSDComputerName = $name
Write-Host “DEBUG – Your Computer Name will be $OSDComputerName”
Return

}

‘7’ {
[System.Windows.MessageBox]::Show(“We will keep the automated name $OSDComputerName”)
Return
}

}

# Make New Computer Name
$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$TSEnv.Value(“OSDComputerName”) = “$OSDComputerName”
$out = $TSEnv.Value(“OSDComputerName”)
Write-Host “DEBUG – OSDComputername is set to $out ”
Rename-Computer -NewName “$OSDComputerName”

Leave a Reply