cschneegans / unattend-generator

.NET Core library to create highly customized autounattend.xml files
https://schneegans.de/windows/unattend-generator/
MIT License
659 stars 60 forks source link

Request - Change Hostname automatically. #70

Closed kendi182 closed 1 month ago

kendi182 commented 1 month ago

``Captura de tela 2024-10-09 105110 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

$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 "") {
    $NewComputerName = $prefix + $serial.Substring(0, 7)
}
else {
    $NewComputerName = $prefix + $UUID.Substring(0, 7)
}

Rename-Computer -NewName $NewComputerName -Force
Restart-Computer -Force
cschneegans commented 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.

cschneegans commented 1 month ago

Just added a new setting Provide a PowerShell script to set the computer name dynamically, which you may find interesting:

image

kendi182 commented 1 month ago

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

cschneegans commented 1 month ago

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)
}
kendi182 commented 1 month ago

But if I don't use the rename command and restart, will the machine name be renamed?

cschneegans commented 1 month ago

Yes. The implementation – mainly the SetComputernName.ps1 script – does not use Rename-Computer or Restart-Computer at all, but simply writes three registry values.

kendi182 commented 1 month ago

thanks! It worked perfectly without having to restart. Thank you for this incredible tool.