Closed kendi182 closed 1 month ago
Could you add this function to the generator?
Your script looks fine, but this is a very specific implementation. This is precisely what custom scripts are for, and I don't think this exact script would be applicable to many people.
Just added a new setting Provide a PowerShell script to set the computer name dynamically, which you may find interesting:
Can I pull the uuid or serial number of the machine in this script? and does it recognize whether the machine is a desktop or notebook?
Thank you
Can I pull the uuid or serial number of the machine in this script?
Yes, the script you posted above will work, just tested it myself, with the result being D-VMware-.
Note that you must not call Rename-Computer
(or Restart-Computer
) yourself, just return the desired name, like so:
$serial = (Get-CimInstance Win32_BIOS).SerialNumber
$UUID = (Get-CimInstance Win32_ComputerSystemProduct).UUID -replace '[^A-F0-9]', ''
$systemType = (Get-CimInstance Win32_ComputerSystem).PCSystemType
$prefix = if ($systemType -eq 1) { 'D-' } elseif ($systemType -eq 2) { 'N-' } else { 'U-' }
if ($serial -and $serial -ne "To be filled by O.E.M." -and $serial -ne "System Serial Number" -and $serial.Trim() -ne "") {
return $prefix + $serial.Substring(0, 7)
} else {
return $prefix + $UUID.Substring(0, 7)
}
But if I don't use the rename command and restart, will the machine name be renamed?
Yes. The implementation – mainly the SetComputernName.ps1 script – does not use Rename-Computer
or Restart-Computer
at all, but simply writes three registry values.
thanks! It worked perfectly without having to restart. Thank you for this incredible tool.
`` Change the hostname based on the machine's UUID or Serial if necessary with the command below. I've already tested it and it really works. Could you add this function to the generator?
Edit: First, test whether the machine has a valid serial number, otherwise it will use the UUID
I modified it to pull D from desktop or N from notebook with an automation script, U for unknown.
Serial first then UUID