rbalsleyMSFT / FFU

Using Full Flash Update files to speed up Windows Deployment
MIT License
70 stars 17 forks source link

Computer Name Length #73

Closed zehadialam closed 3 weeks ago

zehadialam commented 3 weeks ago

Hi Richard,

I've been working on a solution to set computer names longer than 15 characters. We recently encountered an issue where our new lab devices ended up with identical names, as the serial numbers only differed by the last character, which was truncated.

My current implementation involves letting the unattend.xml do the initial naming and then change the name using a SetupComplete.cmd script.

Let me know if you'd be interested in a PR for this solution, or if you have alternative suggestions.

if ($computerName.Length -gt 15) {
    $SetupCompleteData += "`nreg add ""HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"" /v ""NV Hostname"" /t REG_SZ /d ""$computerName"" /f"
    $computerName = $computerName.substring(0, 15)
}
$computerName = Set-Computername($computername)
...
...
New-Item -Path "W:\Windows\Setup\Scripts" -ItemType Directory -Force | Out-Null
Set-Content -Path "W:\Windows\Setup\Scripts\SetupComplete.cmd" -Value $SetupCompleteData -Force

Thank you, Zehadi

HedgeComp commented 3 weeks ago

Hi Zehadi, I had a similar issue but to solve it Simply I made the prefix 1 character shorter or removed the dash "-" to come in under the 15 character limit.

May I ask why you want to have Device Names longer then 15 Characters? I know there are issues with AD and NetBIOS if you go longer. I believe Entra Joined devices doesn't care so much but local AD does.

You could try to trim the computer names from the beginning of the serial Number instead.

$serial = (Get-CimInstance -ClassName win32_bios).SerialNumber.Trim().Substring((Get-CimInstance -ClassName win32_bios).SerialNumber.Trim().Length - 7, 7)

This might work a little for you a little longer but at some point you may run into the same issue with the leading character being dropped when you get batch of devices from the same production run.

-Scott

zehadialam commented 3 weeks ago

Hello Scott. Thank you for your input, however it seems that the most reliable option would be to retain the full computer name. We don't add any devices to an on-prem AD.

rbalsleyMSFT commented 3 weeks ago

I'm not too crazy about setting the name via the registry like that. It wouldn't be a supported method to do it that way. Are you doing it this way to prevent a reboot?

You may want to try using the unattend to run a synchronous command to call Rename-Computer via PowerShell.

https://learn.microsoft.com/en-us/windows-hardware/customize/desktop/unattend/microsoft-windows-deployment-runsynchronous-runsynchronouscommand

zehadialam commented 3 weeks ago

Thanks, that way seems a lot better. I hadn't realized that Rename-Computer accepts names longer than 15 characters.

HedgeComp commented 3 weeks ago

I've done something similar using the Unattend.xml when I was trying to apply a provisioning package that was loaded at Apps Install Phase.

<SynchronousCommand wcm:action="add">
 <CommandLine>powershell.exe -ExecutionPolicy Bypass -File C:\Provisioning\ApplyProvisioningPackage.ps1</CommandLine>
 <Description>Apply Provisioning Package</Description>
 <Order>1</Order>
 </SynchronousCommand>

I am also thinking , couldn't you just simply remove the Trim of 15 IF Statement?

`$computername = ($PrefixToUse + $serial) -replace "\s","" # Remove spaces because windows does not support spaces in the computer names

If computername is longer than 15 characters, reduce to 15. Sysprep/unattend doesn't like ComputerName being longer than 15 characters even though Windows accepts it`

$computername = Set-Computername($computername)

should allow you to have your prefix and keep all the characters of the Serial Number, without the need for RunCommands or registry hacks.

rbalsleyMSFT commented 3 weeks ago

You can't just remove the trim. Unattend doesn't like having names longer than 15 characters. Check the comment you quoted :)

HedgeComp commented 3 weeks ago

You can't just remove the trim. Unattend doesn't like having names longer than 15 characters. Check the comment you quoted :)

.. you are correct sir! I forgot the 15 Character limit is still applied in Unattend.xml . Its been a week..lol

@zehadialam You can also put your code right in the XML.

I use this site for help creating parts of the unattend scripts: https://schneegans.de/windows/unattend-generator/#:~:text=Windows%C2%A011%2023H2.-,Run%20custom%20scripts%3A,-Scripts%20to%20run